Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling Entity Framework proxy creation

From what I've read, setting ProxyCreationEnabled = false will prevent change tracking and lazy loading. However, I'm not clear on what change tracking covers.

If I disable it and get an entity from the database, make changes to it and commit, then those changes are saved. I'm also still able to get modified entries from the ChangeTracker:

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

Should this be possible when I've disabled proxy creation? I want to disable it, but I want to be clear on what I'm disabling.

like image 518
Tom Avatar asked Nov 15 '16 16:11

Tom


People also ask

What is proxy creation in Entity Framework?

When creating instances of POCO entity types, Entity Framework often creates instances of a dynamically generated derived type that acts as a proxy for the entity. This proxy overrides some virtual properties of the entity to insert hooks for performing actions automatically when the property is accessed.

How do I disable POCO proxy creation using DB context?

ContextOptions. ProxyCreationEnabled property is true by default. You need to explicitly set it to "false" in the default constructor of your context object in order to turn this feature off.

What is dynamic proxy entity framework?

Dynamic Proxy IT can also be said that it is a runtime proxy classes like a wrapper class of POCO entity. You can override some properties of the entity for performing actions automatically when the property is accessed. This mechanism is used to support lazy loading of relationships and automatic change tracking.

What is ProxyCreationEnabled?

ProxyCreationEnabled is set to true , child objects will be loaded automatically, and DbContext. Configuration. LazyLoadingEnabled value will control when child objects are loaded.


1 Answers

I can confirm that setting ProxyCreationEnabled to false in EF does not affect Change Tracking. You actually intrigued me with this question as I thought I knew the answer, but to confirm I created a quick test case.

See below example code that represents a valid scenario:

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 disabling proxy generation should affect is the lazy loading functionality of EF when you are using virtual navigation properties to another model. The Change Tracker is independent and works from the underlying ObjectContext itself.

For a complete answer related to change tracking, it's probably worth noting that AutoDetectChangesEnabled appears to be the only setting that would directly affect change tracking functionality, requiring you to call DetectChanges() if you needed to use your sample code.

like image 103
Rudi Visser Avatar answered Sep 28 '22 04:09

Rudi Visser