Disable Entity Framework Proxy Creation

From what I read, setting ProxyCreationEnabled = false prevent change tracking and lazy loading. However, I don’t quite understand how to track changes.

If you disable it and get the entity from the database, make changes to it and commit it, then these changes will be saved. I can also get changed records from ChangeTracker:

 ChangeTracker.Entries<IAuditable>().Where(x => x.State == EntityState.Modified).ToList() 

If possible, when did I turn off proxy creation? I want to disable it, but I want to understand what I disabled.

+8
c # entity-framework
source share
2 answers

Tracking changes and creating proxies are two different scenarios. If you need to disable change tracking, you must do this as shown below.

 public class YourContext : DbContext { public YourContext() { this.Configuration.AutoDetectChangesEnabled = false; } } 

Then you cannot do it ChangeTracker.Entries<IAuditable>().Where(x => x.State == EntityState.Modified).ToList() .

If you need to disable proxy creation, you need to do this in the constructor of your context, as shown below.

 public class YourContext : DbContext { public YourContext() { this.Configuration.ProxyCreationEnabled = false; } public DbSet<Blog> Blogs { get; set; } public DbSet<Post> Posts { get; set; } } 

The proxy engine is used to support lazy loading relationships. EF will not create proxies for types where there is nothing for the proxy. In other words, if you do not have virtual properties in your POCO classes, then there is no effect that you have disabled or not.

If you are serializing your entities, then consider disabling proxies and lazy loading, since proxy deserialization can be complicated.

You can learn more about this here: Entity Framework Working with a proxy

+5
source share

I can confirm that setting ProxyCreationEnabled to false in EF does not affect change tracking. You really intrigued me with this question, because I thought I knew the answer, but to confirm that I created a quick test case.

See below for sample code that represents a valid script:

 namespace EFCTTest { class Program { static void Main(string[] args) { var context = new MyContext(); context.Tests.First().Test = "Edited"; var models = context.ChangeTracker.Entries<TestModel>().Where(x => x.State == EntityState.Modified).ToList(); foreach(var model in models) Console.WriteLine($"From {model.OriginalValues["Test"]} to {model.CurrentValues["Test"]}"); Console.ReadLine(); } } public class MyContext : DbContext { public MyContext() { Configuration.ProxyCreationEnabled = false; } public DbSet<TestModel> Tests { get; set; } } public class TestModel { public int Id { get; set; } public string Test { get; set; } } } 

The only thing that can affect proxy generation is the lazy EF load function when you use the virtual navigation properties for another model. The change tracker is independent and works with the underlying ObjectContext .

For a complete change tracking response, it's probably worth noting that AutoDetectChangesEnabled is the only parameter that will directly affect change tracking functions, requiring you to call DetectChanges() if you need to use your sample code.

+9
source share

All Articles