Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DBContext lazyloadingenabled set to true still loads related entities by default

LazyLoadingEnabled is specifically set to true to prevent the related entities from loading in the context I'm using.

A drug class has a list of drugidentity objects in it.

public class Drug
{
   public virtual List<DrugIdentity> DrugIdentities { get; set; }
}

A specific configuration for the class sets the key and hasmany relationship if I wanted to include the related entity to be loaded.

public DrugConfiguration()
    {
        this.HasKey(d => d.DrugID);
        this.HasMany(d => d.DrugIdentities).WithOptional(d => d.Drug).Map(d => d.MapKey("DrugID"));
    }

When the Drug context is loaded using a linq query the object shows it contains related DrugIdentities when it shouldn't.

context.Configuration.LazyLoadingEnabled = true;

                    var drugs = from d in context.Drug
                                where d.Active == true
                                select d;

drugs[0].DrugIdentities Count = 1

I would expect drugs[0].DrugIdentities to equal NULL since lazyloading was set to true?

like image 794
bretcj7 Avatar asked May 17 '12 22:05

bretcj7


People also ask

How can you disable lazy loading for all entities?

We can disable lazy loading for a particular entity or a context. To turn off lazy loading for a particular property, do not make it virtual. To turn off lazy loading for all entities in the context, set its configuration property to false.

What is LazyLoadingEnabled?

LazyLoadingEnabled is specifically set to true to prevent the related entities from loading in the context I'm using. A drug class has a list of drugidentity objects in it. public class Drug { public virtual List<DrugIdentity> DrugIdentities { get; set; } }

How do I turn off eager loading in Entity Framework?

In EF Core: context. ChangeTracker. LazyLoadingEnabled = false; Per this answer.

What is default loading in Entity Framework?

Hi dsbdev-7403, The default behavior of an Entity Framework is Lazy Loading, where a child entity is loaded only when it is accessed for the first time. It simply delays the loading of the related data, until you ask for it.


2 Answers

To disable lazy loading, set LazyLoadingEnabled to false rather than true. See Lazy, Eager, and Explicit Loading of Related Data in

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application

like image 107
tdykstra Avatar answered Nov 10 '22 13:11

tdykstra


You have to specifically set ProxyCreationEnabled = false if you want to set LazyLoadingEnabled = true.

The test passed on what I expected. The first query returns the Drugs object and NULL for DrugEntities. Second query returns the DrugEntities since I used the Include to do the eager loading.

var queryDrug = from d in context.Drug
                                where d.Active == true
                                select d;

                var drugresults = from d in context.Drug.Include(e => e.DrugIdentities)
                                  where d.Active == true
                                  select d;
like image 34
bretcj7 Avatar answered Nov 10 '22 12:11

bretcj7