Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permanently disable Configuration.ProxyCreationEnabled in EF?

Instead of having to do the following on every query, is there a way to just set that value globally? There is a lazyloading setting in the model view, but there does not seem to be a setting for the ProxyCreation.

        using (var context = new LabEntities())
        {
            **context.Configuration.ProxyCreationEnabled = false;**

            var Query = from s in context.EAssets
                        .Include("Server").Include("Type").Include("Type.Definition")
                        where (s.Type.Definition.b_IsScannable == true) &&
                        (s.Server.s_Domain == Environment.UserDomainName || s.Server.s_Domain == null)
                        select s;
           var Entities = Query.ToList();
        }

I don’t fully understand the benefits of this option, but i know that in visual studio is tags all my objects with gibberish serial suffixes and makes using the debugger unreasonable.

like image 231
jwrightmail Avatar asked Oct 13 '12 00:10

jwrightmail


2 Answers

You can disable it in the constructor, so that it gets disabled anytime you create a new context:

public class LabEntities : DbContext
{
   public LabEntities()
   {
      Configuration.ProxyCreationEnabled = false;
   }
}
like image 51
Mark Oreta Avatar answered Oct 20 '22 00:10

Mark Oreta


If you're using a model-first approach, meaning you have a .edmx file, the way to permanently disable this option is to modify the .Context.tt file. This file is a code generation template that the build process uses to generate your DbContext-derived class.

Open this file and locate the constructor:

public <#=Code.Escape(container)#>()
        : base("name=<#=container.Name#>")
    {
<#
        WriteLazyLoadingEnabled(container);
#>
        //add the following line of code

        this.Configuration.ProxyCreationEnabled = false;
    }

then add the line of code to set this property to false. Rebuild your project and verify the generated context contains the line.

like image 25
Scott Nelson Avatar answered Oct 20 '22 00:10

Scott Nelson