disable dynamic proxy in Entity framework globally

c#, entity-framework

Solution

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;
   }
}

Problem

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

Original source