What difference does .AsNoTracking() make?
I have a question regarding the .AsNoTracking()
extension, as this is all quite new and quite confusing.
I'm using a per-request context for a website.
A lot of my entities don't change so don't need to be tracked, but I have the following scenario where I'm unsure of what's going to the database, or even whether it makes a difference in this case.
This example is what I'm currently doing:
context.Set<User>().AsNoTracking()
// Step 1) Get user
context.Set<User>()
// Step 2) Update user
This is the same as above but removing the .AsNoTracking()
from Step 1:
context.Set<User>();
// Step 1) Get user
context.Set<User>()
// Step 2) Update user
The Steps 1 & 2 use the same context but occur at different times. What I can't work out is whether there is any difference. As Step 2 is an update I'm guessing both will hit the database twice anyway.
Can anyone tell me what the difference is?
c# .net entity-framework entity-framework-4.3
add a comment |
I have a question regarding the .AsNoTracking()
extension, as this is all quite new and quite confusing.
I'm using a per-request context for a website.
A lot of my entities don't change so don't need to be tracked, but I have the following scenario where I'm unsure of what's going to the database, or even whether it makes a difference in this case.
This example is what I'm currently doing:
context.Set<User>().AsNoTracking()
// Step 1) Get user
context.Set<User>()
// Step 2) Update user
This is the same as above but removing the .AsNoTracking()
from Step 1:
context.Set<User>();
// Step 1) Get user
context.Set<User>()
// Step 2) Update user
The Steps 1 & 2 use the same context but occur at different times. What I can't work out is whether there is any difference. As Step 2 is an update I'm guessing both will hit the database twice anyway.
Can anyone tell me what the difference is?
c# .net entity-framework entity-framework-4.3
add a comment |
I have a question regarding the .AsNoTracking()
extension, as this is all quite new and quite confusing.
I'm using a per-request context for a website.
A lot of my entities don't change so don't need to be tracked, but I have the following scenario where I'm unsure of what's going to the database, or even whether it makes a difference in this case.
This example is what I'm currently doing:
context.Set<User>().AsNoTracking()
// Step 1) Get user
context.Set<User>()
// Step 2) Update user
This is the same as above but removing the .AsNoTracking()
from Step 1:
context.Set<User>();
// Step 1) Get user
context.Set<User>()
// Step 2) Update user
The Steps 1 & 2 use the same context but occur at different times. What I can't work out is whether there is any difference. As Step 2 is an update I'm guessing both will hit the database twice anyway.
Can anyone tell me what the difference is?
c# .net entity-framework entity-framework-4.3
I have a question regarding the .AsNoTracking()
extension, as this is all quite new and quite confusing.
I'm using a per-request context for a website.
A lot of my entities don't change so don't need to be tracked, but I have the following scenario where I'm unsure of what's going to the database, or even whether it makes a difference in this case.
This example is what I'm currently doing:
context.Set<User>().AsNoTracking()
// Step 1) Get user
context.Set<User>()
// Step 2) Update user
This is the same as above but removing the .AsNoTracking()
from Step 1:
context.Set<User>();
// Step 1) Get user
context.Set<User>()
// Step 2) Update user
The Steps 1 & 2 use the same context but occur at different times. What I can't work out is whether there is any difference. As Step 2 is an update I'm guessing both will hit the database twice anyway.
Can anyone tell me what the difference is?
c# .net entity-framework entity-framework-4.3
c# .net entity-framework entity-framework-4.3
edited Aug 31 '12 at 8:43
stakx
63.3k15136218
63.3k15136218
asked Aug 31 '12 at 8:35
dotnetnoobdotnetnoob
3,825124280
3,825124280
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
The difference is that in the first case the retrieved user is not tracked by the context so when you are going to save the user back to database you must attach it and set correctly state of the user so that EF knows that it should update existing user instead of inserting a new one. In the second case you don't need to do that if you load and save the user with the same context instance because the tracking mechanism handles that for you.
Can we get the same benefits for anonymous classes in select query, such as context.Users.Select(u=> new { Name = u.Name })? Thanks.
– Dilhan Jayathilake
Feb 6 '17 at 23:33
2
@DilhanJayathilake: Anonymous classes don't represent the entity itself so they don't have tracking.
– Ladislav Mrnka
Feb 13 '17 at 13:42
1
Since EF6 infers the entity key incorrectly on a View sometimes, does AsNoTracking() ignore the key and therefore be an alternative to manually fixing the key (assuming other benefits of the key are not needed).
– crokusek
Nov 7 '17 at 22:51
2
Also note, the biggest effect AsNoTracking has is that lazy loading will not work
– Douglas Gaskell
May 23 '18 at 20:05
add a comment |
see this page Entity Framework and AsNoTracking
What AsNoTracking Does
Entity Framework exposes a number of performance tuning options to help you optimise the performance of your applications. One of these tuning options is .AsNoTracking()
. This optimisation allows you to tell Entity Framework
not to track the results of a query. This means that Entity Framework
performs no additional processing or storage of the entities which are returned by the query. However, it also means that you can't update these entities without reattaching them to the tracking graph.
there are significant performance gains to be had by using AsNoTracking
7
It seems that the gains may be counterbalanced sometimes: stackoverflow.com/questions/9259480/…
– Fabrice
Dec 28 '16 at 0:49
1
My performance gain with a complex query loading a parent child relationship with include in one step was about 50%
– Karl
Jan 19 '18 at 6:33
add a comment |
No Tracking LINQ to Entities queries
Usage of AsNoTracking() is recommended when your query is meant for read operations. In these scenarios, you get back your entities but they are not tracked by your context.This ensures minimal memory usage and optimal performance
Pros
- Improved performance over regular LINQ queries.
- Fully materialized objects.
- Simplest to write with syntax built into the programming
language.
Cons
- Not suitable for CUD operations.
- Certain technical restrictions, such as: Patterns using DefaultIfEmpty for
OUTER JOIN queries result in more complex queries than simple OUTER
JOIN statements in Entity SQL.
- You still can’t use LIKE with general pattern matching.
More info available here:
Performance considerations for Entity Framework
Entity Framework and NoTracking
add a comment |
Disabling tracking will also cause your result sets to be streamed into memory. This is more efficient when you're working with large sets of data and don't need the entire set of data all at once.
References:
- How to avoid memory overflow when querying large datasets with Entity Framework and LINQ
- Entity framework large data set, out of memory exception
add a comment |
AsNoTracking() allows the "unique key per record" requirement in EF to be bypassed (not mentioned explicitly by other answers).
This is extremely helpful when reading a View that does not support a unique key because perhaps some fields are nullable or the nature of the view is not logically indexable.
For these cases the "key" can be set to any non-nullable column but then AsNoTracking() must be used with every query else records (duplicate by key) will be skipped.
2
Just to reiterate the importance of this with Views, I have a query from a view which returns 7 unique records when run via SSMS. When run via EF, without the AsNoTracking modifier, I get the first record, three copies of the second and three copies of the third. This took a lot of incredulous head-scratching to fix, and it was using AsNoTracking which fixed it!
– Ade
Mar 22 '18 at 14:26
I had this exact same issue when using Linq to Entities while querying a View with no primary keys. Only found out about AsNoTracking after half a day of head-scratching. This ASP.Net forum post eventually led me to it. forums.asp.net/t/…
– red_dorian
Sep 14 '18 at 15:35
add a comment |
If you have something else altering the DB (say another process) and need to ensure you see these changes, use AsNoTracking()
, otherwise EF may give you the last copy that your context had instead, hence it being good to usually use a new context every query:
http://codethug.com/2016/02/19/Entity-Framework-Cache-Busting/
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12211680%2fwhat-difference-does-asnotracking-make%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
The difference is that in the first case the retrieved user is not tracked by the context so when you are going to save the user back to database you must attach it and set correctly state of the user so that EF knows that it should update existing user instead of inserting a new one. In the second case you don't need to do that if you load and save the user with the same context instance because the tracking mechanism handles that for you.
Can we get the same benefits for anonymous classes in select query, such as context.Users.Select(u=> new { Name = u.Name })? Thanks.
– Dilhan Jayathilake
Feb 6 '17 at 23:33
2
@DilhanJayathilake: Anonymous classes don't represent the entity itself so they don't have tracking.
– Ladislav Mrnka
Feb 13 '17 at 13:42
1
Since EF6 infers the entity key incorrectly on a View sometimes, does AsNoTracking() ignore the key and therefore be an alternative to manually fixing the key (assuming other benefits of the key are not needed).
– crokusek
Nov 7 '17 at 22:51
2
Also note, the biggest effect AsNoTracking has is that lazy loading will not work
– Douglas Gaskell
May 23 '18 at 20:05
add a comment |
The difference is that in the first case the retrieved user is not tracked by the context so when you are going to save the user back to database you must attach it and set correctly state of the user so that EF knows that it should update existing user instead of inserting a new one. In the second case you don't need to do that if you load and save the user with the same context instance because the tracking mechanism handles that for you.
Can we get the same benefits for anonymous classes in select query, such as context.Users.Select(u=> new { Name = u.Name })? Thanks.
– Dilhan Jayathilake
Feb 6 '17 at 23:33
2
@DilhanJayathilake: Anonymous classes don't represent the entity itself so they don't have tracking.
– Ladislav Mrnka
Feb 13 '17 at 13:42
1
Since EF6 infers the entity key incorrectly on a View sometimes, does AsNoTracking() ignore the key and therefore be an alternative to manually fixing the key (assuming other benefits of the key are not needed).
– crokusek
Nov 7 '17 at 22:51
2
Also note, the biggest effect AsNoTracking has is that lazy loading will not work
– Douglas Gaskell
May 23 '18 at 20:05
add a comment |
The difference is that in the first case the retrieved user is not tracked by the context so when you are going to save the user back to database you must attach it and set correctly state of the user so that EF knows that it should update existing user instead of inserting a new one. In the second case you don't need to do that if you load and save the user with the same context instance because the tracking mechanism handles that for you.
The difference is that in the first case the retrieved user is not tracked by the context so when you are going to save the user back to database you must attach it and set correctly state of the user so that EF knows that it should update existing user instead of inserting a new one. In the second case you don't need to do that if you load and save the user with the same context instance because the tracking mechanism handles that for you.
edited Aug 17 '15 at 15:08
Jess
12.3k1081112
12.3k1081112
answered Aug 31 '12 at 8:39
Ladislav MrnkaLadislav Mrnka
325k54614640
325k54614640
Can we get the same benefits for anonymous classes in select query, such as context.Users.Select(u=> new { Name = u.Name })? Thanks.
– Dilhan Jayathilake
Feb 6 '17 at 23:33
2
@DilhanJayathilake: Anonymous classes don't represent the entity itself so they don't have tracking.
– Ladislav Mrnka
Feb 13 '17 at 13:42
1
Since EF6 infers the entity key incorrectly on a View sometimes, does AsNoTracking() ignore the key and therefore be an alternative to manually fixing the key (assuming other benefits of the key are not needed).
– crokusek
Nov 7 '17 at 22:51
2
Also note, the biggest effect AsNoTracking has is that lazy loading will not work
– Douglas Gaskell
May 23 '18 at 20:05
add a comment |
Can we get the same benefits for anonymous classes in select query, such as context.Users.Select(u=> new { Name = u.Name })? Thanks.
– Dilhan Jayathilake
Feb 6 '17 at 23:33
2
@DilhanJayathilake: Anonymous classes don't represent the entity itself so they don't have tracking.
– Ladislav Mrnka
Feb 13 '17 at 13:42
1
Since EF6 infers the entity key incorrectly on a View sometimes, does AsNoTracking() ignore the key and therefore be an alternative to manually fixing the key (assuming other benefits of the key are not needed).
– crokusek
Nov 7 '17 at 22:51
2
Also note, the biggest effect AsNoTracking has is that lazy loading will not work
– Douglas Gaskell
May 23 '18 at 20:05
Can we get the same benefits for anonymous classes in select query, such as context.Users.Select(u=> new { Name = u.Name })? Thanks.
– Dilhan Jayathilake
Feb 6 '17 at 23:33
Can we get the same benefits for anonymous classes in select query, such as context.Users.Select(u=> new { Name = u.Name })? Thanks.
– Dilhan Jayathilake
Feb 6 '17 at 23:33
2
2
@DilhanJayathilake: Anonymous classes don't represent the entity itself so they don't have tracking.
– Ladislav Mrnka
Feb 13 '17 at 13:42
@DilhanJayathilake: Anonymous classes don't represent the entity itself so they don't have tracking.
– Ladislav Mrnka
Feb 13 '17 at 13:42
1
1
Since EF6 infers the entity key incorrectly on a View sometimes, does AsNoTracking() ignore the key and therefore be an alternative to manually fixing the key (assuming other benefits of the key are not needed).
– crokusek
Nov 7 '17 at 22:51
Since EF6 infers the entity key incorrectly on a View sometimes, does AsNoTracking() ignore the key and therefore be an alternative to manually fixing the key (assuming other benefits of the key are not needed).
– crokusek
Nov 7 '17 at 22:51
2
2
Also note, the biggest effect AsNoTracking has is that lazy loading will not work
– Douglas Gaskell
May 23 '18 at 20:05
Also note, the biggest effect AsNoTracking has is that lazy loading will not work
– Douglas Gaskell
May 23 '18 at 20:05
add a comment |
see this page Entity Framework and AsNoTracking
What AsNoTracking Does
Entity Framework exposes a number of performance tuning options to help you optimise the performance of your applications. One of these tuning options is .AsNoTracking()
. This optimisation allows you to tell Entity Framework
not to track the results of a query. This means that Entity Framework
performs no additional processing or storage of the entities which are returned by the query. However, it also means that you can't update these entities without reattaching them to the tracking graph.
there are significant performance gains to be had by using AsNoTracking
7
It seems that the gains may be counterbalanced sometimes: stackoverflow.com/questions/9259480/…
– Fabrice
Dec 28 '16 at 0:49
1
My performance gain with a complex query loading a parent child relationship with include in one step was about 50%
– Karl
Jan 19 '18 at 6:33
add a comment |
see this page Entity Framework and AsNoTracking
What AsNoTracking Does
Entity Framework exposes a number of performance tuning options to help you optimise the performance of your applications. One of these tuning options is .AsNoTracking()
. This optimisation allows you to tell Entity Framework
not to track the results of a query. This means that Entity Framework
performs no additional processing or storage of the entities which are returned by the query. However, it also means that you can't update these entities without reattaching them to the tracking graph.
there are significant performance gains to be had by using AsNoTracking
7
It seems that the gains may be counterbalanced sometimes: stackoverflow.com/questions/9259480/…
– Fabrice
Dec 28 '16 at 0:49
1
My performance gain with a complex query loading a parent child relationship with include in one step was about 50%
– Karl
Jan 19 '18 at 6:33
add a comment |
see this page Entity Framework and AsNoTracking
What AsNoTracking Does
Entity Framework exposes a number of performance tuning options to help you optimise the performance of your applications. One of these tuning options is .AsNoTracking()
. This optimisation allows you to tell Entity Framework
not to track the results of a query. This means that Entity Framework
performs no additional processing or storage of the entities which are returned by the query. However, it also means that you can't update these entities without reattaching them to the tracking graph.
there are significant performance gains to be had by using AsNoTracking
see this page Entity Framework and AsNoTracking
What AsNoTracking Does
Entity Framework exposes a number of performance tuning options to help you optimise the performance of your applications. One of these tuning options is .AsNoTracking()
. This optimisation allows you to tell Entity Framework
not to track the results of a query. This means that Entity Framework
performs no additional processing or storage of the entities which are returned by the query. However, it also means that you can't update these entities without reattaching them to the tracking graph.
there are significant performance gains to be had by using AsNoTracking
edited Nov 13 '18 at 11:24
answered Apr 2 '14 at 9:26
MojiMoji
3,20822628
3,20822628
7
It seems that the gains may be counterbalanced sometimes: stackoverflow.com/questions/9259480/…
– Fabrice
Dec 28 '16 at 0:49
1
My performance gain with a complex query loading a parent child relationship with include in one step was about 50%
– Karl
Jan 19 '18 at 6:33
add a comment |
7
It seems that the gains may be counterbalanced sometimes: stackoverflow.com/questions/9259480/…
– Fabrice
Dec 28 '16 at 0:49
1
My performance gain with a complex query loading a parent child relationship with include in one step was about 50%
– Karl
Jan 19 '18 at 6:33
7
7
It seems that the gains may be counterbalanced sometimes: stackoverflow.com/questions/9259480/…
– Fabrice
Dec 28 '16 at 0:49
It seems that the gains may be counterbalanced sometimes: stackoverflow.com/questions/9259480/…
– Fabrice
Dec 28 '16 at 0:49
1
1
My performance gain with a complex query loading a parent child relationship with include in one step was about 50%
– Karl
Jan 19 '18 at 6:33
My performance gain with a complex query loading a parent child relationship with include in one step was about 50%
– Karl
Jan 19 '18 at 6:33
add a comment |
No Tracking LINQ to Entities queries
Usage of AsNoTracking() is recommended when your query is meant for read operations. In these scenarios, you get back your entities but they are not tracked by your context.This ensures minimal memory usage and optimal performance
Pros
- Improved performance over regular LINQ queries.
- Fully materialized objects.
- Simplest to write with syntax built into the programming
language.
Cons
- Not suitable for CUD operations.
- Certain technical restrictions, such as: Patterns using DefaultIfEmpty for
OUTER JOIN queries result in more complex queries than simple OUTER
JOIN statements in Entity SQL.
- You still can’t use LIKE with general pattern matching.
More info available here:
Performance considerations for Entity Framework
Entity Framework and NoTracking
add a comment |
No Tracking LINQ to Entities queries
Usage of AsNoTracking() is recommended when your query is meant for read operations. In these scenarios, you get back your entities but they are not tracked by your context.This ensures minimal memory usage and optimal performance
Pros
- Improved performance over regular LINQ queries.
- Fully materialized objects.
- Simplest to write with syntax built into the programming
language.
Cons
- Not suitable for CUD operations.
- Certain technical restrictions, such as: Patterns using DefaultIfEmpty for
OUTER JOIN queries result in more complex queries than simple OUTER
JOIN statements in Entity SQL.
- You still can’t use LIKE with general pattern matching.
More info available here:
Performance considerations for Entity Framework
Entity Framework and NoTracking
add a comment |
No Tracking LINQ to Entities queries
Usage of AsNoTracking() is recommended when your query is meant for read operations. In these scenarios, you get back your entities but they are not tracked by your context.This ensures minimal memory usage and optimal performance
Pros
- Improved performance over regular LINQ queries.
- Fully materialized objects.
- Simplest to write with syntax built into the programming
language.
Cons
- Not suitable for CUD operations.
- Certain technical restrictions, such as: Patterns using DefaultIfEmpty for
OUTER JOIN queries result in more complex queries than simple OUTER
JOIN statements in Entity SQL.
- You still can’t use LIKE with general pattern matching.
More info available here:
Performance considerations for Entity Framework
Entity Framework and NoTracking
No Tracking LINQ to Entities queries
Usage of AsNoTracking() is recommended when your query is meant for read operations. In these scenarios, you get back your entities but they are not tracked by your context.This ensures minimal memory usage and optimal performance
Pros
- Improved performance over regular LINQ queries.
- Fully materialized objects.
- Simplest to write with syntax built into the programming
language.
Cons
- Not suitable for CUD operations.
- Certain technical restrictions, such as: Patterns using DefaultIfEmpty for
OUTER JOIN queries result in more complex queries than simple OUTER
JOIN statements in Entity SQL.
- You still can’t use LIKE with general pattern matching.
More info available here:
Performance considerations for Entity Framework
Entity Framework and NoTracking
edited Nov 9 '18 at 10:13
Bernard Vander Beken
2,59443056
2,59443056
answered Jun 13 '16 at 13:06
NullReferenceNullReference
1,4281828
1,4281828
add a comment |
add a comment |
Disabling tracking will also cause your result sets to be streamed into memory. This is more efficient when you're working with large sets of data and don't need the entire set of data all at once.
References:
- How to avoid memory overflow when querying large datasets with Entity Framework and LINQ
- Entity framework large data set, out of memory exception
add a comment |
Disabling tracking will also cause your result sets to be streamed into memory. This is more efficient when you're working with large sets of data and don't need the entire set of data all at once.
References:
- How to avoid memory overflow when querying large datasets with Entity Framework and LINQ
- Entity framework large data set, out of memory exception
add a comment |
Disabling tracking will also cause your result sets to be streamed into memory. This is more efficient when you're working with large sets of data and don't need the entire set of data all at once.
References:
- How to avoid memory overflow when querying large datasets with Entity Framework and LINQ
- Entity framework large data set, out of memory exception
Disabling tracking will also cause your result sets to be streamed into memory. This is more efficient when you're working with large sets of data and don't need the entire set of data all at once.
References:
- How to avoid memory overflow when querying large datasets with Entity Framework and LINQ
- Entity framework large data set, out of memory exception
edited May 23 '17 at 11:54
Community♦
11
11
answered Dec 7 '13 at 2:03
Ronnie OverbyRonnie Overby
23.6k59228324
23.6k59228324
add a comment |
add a comment |
AsNoTracking() allows the "unique key per record" requirement in EF to be bypassed (not mentioned explicitly by other answers).
This is extremely helpful when reading a View that does not support a unique key because perhaps some fields are nullable or the nature of the view is not logically indexable.
For these cases the "key" can be set to any non-nullable column but then AsNoTracking() must be used with every query else records (duplicate by key) will be skipped.
2
Just to reiterate the importance of this with Views, I have a query from a view which returns 7 unique records when run via SSMS. When run via EF, without the AsNoTracking modifier, I get the first record, three copies of the second and three copies of the third. This took a lot of incredulous head-scratching to fix, and it was using AsNoTracking which fixed it!
– Ade
Mar 22 '18 at 14:26
I had this exact same issue when using Linq to Entities while querying a View with no primary keys. Only found out about AsNoTracking after half a day of head-scratching. This ASP.Net forum post eventually led me to it. forums.asp.net/t/…
– red_dorian
Sep 14 '18 at 15:35
add a comment |
AsNoTracking() allows the "unique key per record" requirement in EF to be bypassed (not mentioned explicitly by other answers).
This is extremely helpful when reading a View that does not support a unique key because perhaps some fields are nullable or the nature of the view is not logically indexable.
For these cases the "key" can be set to any non-nullable column but then AsNoTracking() must be used with every query else records (duplicate by key) will be skipped.
2
Just to reiterate the importance of this with Views, I have a query from a view which returns 7 unique records when run via SSMS. When run via EF, without the AsNoTracking modifier, I get the first record, three copies of the second and three copies of the third. This took a lot of incredulous head-scratching to fix, and it was using AsNoTracking which fixed it!
– Ade
Mar 22 '18 at 14:26
I had this exact same issue when using Linq to Entities while querying a View with no primary keys. Only found out about AsNoTracking after half a day of head-scratching. This ASP.Net forum post eventually led me to it. forums.asp.net/t/…
– red_dorian
Sep 14 '18 at 15:35
add a comment |
AsNoTracking() allows the "unique key per record" requirement in EF to be bypassed (not mentioned explicitly by other answers).
This is extremely helpful when reading a View that does not support a unique key because perhaps some fields are nullable or the nature of the view is not logically indexable.
For these cases the "key" can be set to any non-nullable column but then AsNoTracking() must be used with every query else records (duplicate by key) will be skipped.
AsNoTracking() allows the "unique key per record" requirement in EF to be bypassed (not mentioned explicitly by other answers).
This is extremely helpful when reading a View that does not support a unique key because perhaps some fields are nullable or the nature of the view is not logically indexable.
For these cases the "key" can be set to any non-nullable column but then AsNoTracking() must be used with every query else records (duplicate by key) will be skipped.
answered Feb 6 '18 at 23:09
crokusekcrokusek
2,97412742
2,97412742
2
Just to reiterate the importance of this with Views, I have a query from a view which returns 7 unique records when run via SSMS. When run via EF, without the AsNoTracking modifier, I get the first record, three copies of the second and three copies of the third. This took a lot of incredulous head-scratching to fix, and it was using AsNoTracking which fixed it!
– Ade
Mar 22 '18 at 14:26
I had this exact same issue when using Linq to Entities while querying a View with no primary keys. Only found out about AsNoTracking after half a day of head-scratching. This ASP.Net forum post eventually led me to it. forums.asp.net/t/…
– red_dorian
Sep 14 '18 at 15:35
add a comment |
2
Just to reiterate the importance of this with Views, I have a query from a view which returns 7 unique records when run via SSMS. When run via EF, without the AsNoTracking modifier, I get the first record, three copies of the second and three copies of the third. This took a lot of incredulous head-scratching to fix, and it was using AsNoTracking which fixed it!
– Ade
Mar 22 '18 at 14:26
I had this exact same issue when using Linq to Entities while querying a View with no primary keys. Only found out about AsNoTracking after half a day of head-scratching. This ASP.Net forum post eventually led me to it. forums.asp.net/t/…
– red_dorian
Sep 14 '18 at 15:35
2
2
Just to reiterate the importance of this with Views, I have a query from a view which returns 7 unique records when run via SSMS. When run via EF, without the AsNoTracking modifier, I get the first record, three copies of the second and three copies of the third. This took a lot of incredulous head-scratching to fix, and it was using AsNoTracking which fixed it!
– Ade
Mar 22 '18 at 14:26
Just to reiterate the importance of this with Views, I have a query from a view which returns 7 unique records when run via SSMS. When run via EF, without the AsNoTracking modifier, I get the first record, three copies of the second and three copies of the third. This took a lot of incredulous head-scratching to fix, and it was using AsNoTracking which fixed it!
– Ade
Mar 22 '18 at 14:26
I had this exact same issue when using Linq to Entities while querying a View with no primary keys. Only found out about AsNoTracking after half a day of head-scratching. This ASP.Net forum post eventually led me to it. forums.asp.net/t/…
– red_dorian
Sep 14 '18 at 15:35
I had this exact same issue when using Linq to Entities while querying a View with no primary keys. Only found out about AsNoTracking after half a day of head-scratching. This ASP.Net forum post eventually led me to it. forums.asp.net/t/…
– red_dorian
Sep 14 '18 at 15:35
add a comment |
If you have something else altering the DB (say another process) and need to ensure you see these changes, use AsNoTracking()
, otherwise EF may give you the last copy that your context had instead, hence it being good to usually use a new context every query:
http://codethug.com/2016/02/19/Entity-Framework-Cache-Busting/
add a comment |
If you have something else altering the DB (say another process) and need to ensure you see these changes, use AsNoTracking()
, otherwise EF may give you the last copy that your context had instead, hence it being good to usually use a new context every query:
http://codethug.com/2016/02/19/Entity-Framework-Cache-Busting/
add a comment |
If you have something else altering the DB (say another process) and need to ensure you see these changes, use AsNoTracking()
, otherwise EF may give you the last copy that your context had instead, hence it being good to usually use a new context every query:
http://codethug.com/2016/02/19/Entity-Framework-Cache-Busting/
If you have something else altering the DB (say another process) and need to ensure you see these changes, use AsNoTracking()
, otherwise EF may give you the last copy that your context had instead, hence it being good to usually use a new context every query:
http://codethug.com/2016/02/19/Entity-Framework-Cache-Busting/
edited Jan 15 '18 at 15:12
Housy
19.3k65095
19.3k65095
answered Jul 4 '17 at 14:50
andrew pateandrew pate
1,345147
1,345147
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12211680%2fwhat-difference-does-asnotracking-make%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown