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?
The AsNoTracking method tells Entity Framework to stop that additional work and so, it can improve the performance of your application.
AsNoTracking(IQueryable)Returns a new query where the entities returned will not be cached in the DbContext or ObjectContext. This method works by calling the AsNoTracking method of the underlying query object.
The AsNoTracking() method returns a new query where the change tracker will not track any of the entities that are returned. If the entity instances are modified, this will not be detected by the change tracker, and SaveChanges() will not persist those changes to the database.
It means that EF does not perform any additional task to store the retrieve entities for tracking. We can also change the default behavior of tracking at context instance level.
AsNoTracking. The AsNoTracking () method returns a new query where the change tracker will not track any of the entities that are returned. If the entity instances are modified, this will not be detected by the change tracker, and SaveChanges () will not persist those changes to the database.
AsNoTracking () is a powerful method from DbExtensions class. And for sure – the method you should know and use ?. Why should I use it? In short – when we call AsNoTracking (), less data is cached and tracked (we will talk about it later). And of course, the more records we retrieve from the database, the more RAM we spare. How to use it?
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.
In Entity Framework Core, when doing a LINQ query with the AsNoTracking method, all entities are not part of the ChangeTracker. So, if you want to delete an entity, you need first to Attach it to the context and change the EntityState to Deleted. How to use AsNoTracking and Insert an entity?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With