11

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.

jwrightmail
  • 907
  • 1
  • 13
  • 24

2 Answers2

26

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;
   }
}
Mark Oreta
  • 10,346
  • 1
  • 33
  • 36
  • 8
    What about the problem reguarding the automatic file generation that happends if you refresh the model? Wont the manual edits get over writen? – jwrightmail Oct 14 '12 at 23:00
  • What problem with the automatic file generation? – Mark Oreta Oct 14 '12 at 23:21
  • 1
    In order to disable it in the constructor, you have to edit an automaticlly generated file. In my case, its datamodel.context.cs. // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // – jwrightmail Oct 15 '12 at 00:24
  • 6
    Oh you're using an edmx - The entities creates a partial OnContextCreated() - you can put the Configuration.ProxyCreationEnabled in that, in a buddy class so that it doesn't get overwritten – Mark Oreta Oct 15 '12 at 00:29
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.

Scott Nelson
  • 223
  • 1
  • 5
  • 2
    This is the correct answer in my view. Be careful, though, if upgrade Entity Framework in future as it may overwrite your changes to the .tt file. (Unless I'm mistaken?) – rwalter Nov 11 '13 at 14:46
  • Always wondered about that...I would love to see an answer that extends the generated partial class using DbContext (which doesn't define `OnContextCreated`). – seebiscuit May 05 '17 at 21:55