disable dynamic proxy in Entity framework globally

c# entity-framework

Question

Please how can I disable dynamic proxies for all entities created in Entity Framework 5.

Currently, I am setting this espEntities.Configuration.ProxyCreationEnabled = false; in every instance of a DbContext is there a way I can do this for current and future models as a one time task.

Thanks

1
15
3/20/2018 10:32:38 PM

Accepted Answer

Method 1

If you have an EDMX model, a partial class is created. Use that and in the OnContextCreated you can disable ProxyCreationEnabled

public partial class MyModelContainer
{
    public void OnContextCreated()
    {
        this.Configuration.ContextOptions.ProxyCreationEnabled = false;
    }
}

Method 2

Edit the model.tt file. Find the line containing something like this:

partial class <#=code.Escape(container)#> : DbContext

And add in

this.Configuration.ProxyCreationEnabled = false;

Method 3

If you are not using an EDMX file, do it in your context constructor: (assuming your context is called EspEntities)

public class EspEntities : DbContext
{
   public EspEntities()
   {
      Configuration.ProxyCreationEnabled = false;
   }
}
26
7/11/2016 9:01:14 AM


Related Questions





Related

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