benjovi
benjovi

Reputation: 13

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

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?

Upvotes: 1

Views: 4454

Answers (1)

user1011627
user1011627

Reputation: 1811

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

Upvotes: 1

Related Questions