disable dynamic proxy in Entity framework globally

c# entity-framework

Question

Please let me know how I may turn off dynamic proxies for all Entity Framework 5 entities.

Right now, I'm setting thisespEntities.Configuration.ProxyCreationEnabled = false; every time there is aDbContext Is there a way I can complete this as a one-time task for both present and future models.

Thanks

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

Accepted Answer

Approach 1

You can create a partial class if you have an EDMX model. Utilize that and theOnContextCreated You may turn offProxyCreationEnabled

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

Approach 2

Modify the file model.tt. Look for the line that begins with something like this:

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

And include

this.Configuration.ProxyCreationEnabled = false;

Approach 3

If an EDMX file is not being used, complete the following 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