Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable dynamic proxy in Entity framework globally

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

like image 908
user989865 Avatar asked Jul 10 '14 10:07

user989865


1 Answers

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;
   }
}
like image 61
DavidG Avatar answered Oct 08 '22 02:10

DavidG