0

I know based on several articles from MSDN that you simply add:

this.Configuration.LazyLoadingEnabled = false;
this.Configuration.ProxyCreationEnabled = false;

In the context.cs file that accompanies the EDMX file or edit context.tt file to have those lines added when you regen the edmx files.

However, I have noticed that when the database fields change (specifically deleting them), and the ADO.NET files are replaced, the changes have to be remade. Is there a place I can permanently add those lines to be included with my context.cs file when I have to recreate the ADO.NET files?

I did notice that this article on SO is close to this issue, but no where near:

Disable lazy loading by default in Entity Framework 4

Community
  • 1
  • 1
John Schultz
  • 672
  • 1
  • 10
  • 29
  • Are you doing database first, design first, or code first? – mason Dec 15 '15 at 22:24
  • From what I can tell that link talks about code first. Is it the same for database first? – John Schultz Dec 15 '15 at 22:24
  • I definately misread part of that article. However when I do have to readd my edmx, the context file is recreated. is there a way to create an "addition" dbcontext file so I do not have to edit it everytime? – John Schultz Dec 15 '15 at 22:26
  • The SO thread you linked has the solution: 'the EntityContainer element in the edmx:ConceptualModels section of the EDMX file should be edited by adding the annotation:LazyLoadingEnabled="false" attribute - either manually in an XML editor or on the properties page of the designer surface where this option is available as well.' – devlin carnate Dec 15 '15 at 22:34
  • umm... sort of. I need to be able to create the edmx with the two lines automatically being added. When the edmx is regenerated, the context file is wiped and recreated. – John Schultz Dec 15 '15 at 22:35
  • is there a way to have a class overwrite the entities entry in the context file? – John Schultz Dec 15 '15 at 22:36
  • 'This modification of the EDMX file will automatically generate the context class with the disabled lazy loading option in the constructor like shown above. The EDMX file modification itself does not get overwritten when the model is updated from the database.' -- i haven't tested this myself, but this solution seems to have worked for some people – devlin carnate Dec 15 '15 at 22:37

2 Answers2

5

The class that EDMX generate is partial, so you can write code in this class in another file.

So you will create another file, and its content you will "create" same class like that:

namespace Same.Namespace.FromOtherContextClass
{
    public partial class Context : DbContext
    {
        public Context()
        {
            this.Configuration.LazyLoadingEnabled = false;
            this.Configuration.ProxyCreationEnabled = false;
        }
    }
}

Remember, you must have same namespace in this file.

Alberto Monteiro
  • 5,989
  • 2
  • 28
  • 40
  • I tried, but it says "A constructor with same parameter is already present", and also can't override since it's a partial class. Could you please tell how to use it. – its4zahoor Feb 21 '19 at 14:31
-3

@AlbertoMonterio had the right idea.

here is the code that works:

using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using CorporateWeb.API.Model;

namespace CorporateWeb.API.DAL
{
    public partial class context : DbContext
    {
        public context() : base("name=Corporate_WebEntities")
        {
            this.Configuration.LazyLoadingEnabled = false;
            this.Configuration.ProxyCreationEnabled = false;
        }
    }
}
John Schultz
  • 672
  • 1
  • 10
  • 29
  • Your answer does not work. What I posted is the correct answer. – John Schultz Dec 16 '15 at 07:26
  • The code in my answer is just a **sample**, and I said that "same class **like that**". My answer is the right, because I say you to use partial class. Most of answers in stackoverflow that contains a code are just a sample, and not final code. How could I create the right code? I dont know your namespace, classe name, and worse, connection string name. – Alberto Monteiro Dec 16 '15 at 10:16
  • That is not the part that does not work. I understand the concept of "same class...". I do not have the time or energy to have to point out what is wrong in someones example. Also I am not in the habit of accepting something that fails a simple codeing test. – John Schultz Dec 17 '15 at 05:43
  • You do not have condition to argue about that, because you are wrong, can you see that you have 3 downvotes? The solution to your problem, is use a partial class and define two lines in your question inside the constructor defined in partial class, and I showed it in my answer. But I dont care about it anymore, you dont have Stackoverflow feeling. – Alberto Monteiro Dec 17 '15 at 08:45
  • This is becoming pointless and going nowhere. in your **ORIGINAL** post, you suggest using the `static` keyword. You CANNOT use `this` inside a static as VS'15 will tell you. I suspect that your ego has gotten the better of you. As I suspect that you may have down voted my **CORRECT** answer. I see now that even certain individuals can not handle constructive criticism as they tend to out-lash at others. Frankly, I could give a rats butt about the down votes as I do not care anymore. – John Schultz Dec 17 '15 at 09:02
  • Yeah, I used static in my first code, you really think that I dont know that I cant use this with static? Could you ever know that I typed it wrong? I was not using VS to write this code, I was in sublime, it was just a simple mistake, that I already fixed. – Alberto Monteiro Dec 17 '15 at 09:05