Permanently disable Configuration.ProxyCreationEnabled in EF?

后端 未结 2 794
生来不讨喜
生来不讨喜 2020-12-19 01:48

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 se

相关标签:
2条回答
  • 2020-12-19 02:08

    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;
       }
    }
    
    0 讨论(0)
  • 2020-12-19 02:17

    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.

    0 讨论(0)
提交回复
热议问题