Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does getting entities with AsNoTracking() disable the automatic call to DetectChanges()?

I've come to know this concept of AsNoTracking(), DetectChanges(), and AutoDetectChangesEnabled very recently. I understand that when fetching records from the database via Entity Framework with AsNoTracking() used, then Entity Framework does not track any changes on those records and updating any property of the fetched record will fail in that case.

My question is if records are fetched in that manner, will it also cause disabling the automatic call to DetectChanges() or does that have to be done explicitly by setting:

Context.Configuration.AutoDetectChangesEnabled = false; 

Also kindly let me know what impact (in terms of performance) does it have if both of the actions are performed while fetching the data strictly for read only purposes:

Context.Configuration.AutoDetectChangesEnabled = false; Context.Set<T>().AsNoTracking(); 
like image 245
Sayan Pal Avatar asked Jan 08 '14 06:01

Sayan Pal


People also ask

What difference does AsNoTracking () make?

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.

What is the use of AsNoTracking in Entity Framework?

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.

What is AsNoTracking in Entity Framework Core?

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.

How do Detectchanges work?

Detect Changes works by detecting the differences between the current property values of the entity and the original property values that are stored in a snapshot when the entity was queried or attached.


1 Answers

will it also cause disabling the automatic call to DetectChanges()

No it won't. But you must realize that AsNoTracking and DetectChanges have nothing to do with each other (apart from being part of EF). Objects fetched with AsNoTracking will never be change detected anyway, whether AutoDetectChanges is enabled or not. Besides, AsNoTracking works on a DbSet level, AutoDetectChangesEnabled on the context level. It would be bad to have a DbSet method affect the whole context.

or that [setting AutoDetectChangesEnabled] has to be done explicitly

Well, you probably just shouldn't disable AutoDetectChanges. If you do it you must know what you do.

what impact(in terms of performance) does it have if both of the action is performed

As said, they are not related. They can both improve performance in their own way.

  • AsNoTracking is great if you want to fetch read-only data. It has no side effects (as in: its effect is clear)
  • Setting AutoDetectChangesEnabled = false stops automatic calls of DetectChanges (which can be numerous), but it has side effects you need to be aware of. From Lerman & Miller's book DbContext:

    Working out when DetectChanges needs to be called isn’t as trivial as it may appear. The Entity Framework team strongly recommends that you only swap to manually calling DetectChanges if you are experiencing performance issues. It’s also recommended to only opt out of automatic DetectChanges for poorly performing sections of code and to reenable it once the section in question has finished executing.

like image 71
Gert Arnold Avatar answered Oct 25 '22 15:10

Gert Arnold