Entity Framework 6: Updating entity when tracking is disabled on context

c# dbcontext entity-framework entity-framework-6


I'm hoping I can get some clarification on what the best practices are for using a dbcontext with tracking disabled. What are the implications on setting the context.Configuration.ProxyCreationEnabled to false?

As I understand it, this will effectively disable tracking on entities, but what if I then need to SaveChanges with this context? I think I remember seeing a way to get the context to track a specific entity manually, but I am unable to find more information on that. What are the performance implications for calling SaveChanges on a context that has proxy creation disabled?

I also have similar concerns with using no-tracking queries as well. Is it possible to SaveChanges after retrieving data with AsNoTracking?

10/11/2018 2:19:53 PM

Accepted Answer

Without ProxyCreationEnabled, EF will not eagerly load child entities for your object without explicitly "Including" them and change tracking will be disabled as well.

If you use AsNoTracking, you will have to set the entities states to Modified on the object context in order to persist changes. You can do that with code like this:

context.Entry(entity).State = EntityState.Modified;

Here are a couple of other SO questions that go deeper into your question.

What are the downsides to turning off ProxyCreationEnabled for CTP5 of EF code first

DbSet.Attach(entity) vs DbContext.Entry(entity).State = EntityState.Modified

10/11/2018 2:43:48 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow