6

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?

bretcj7
  • 355
  • 2
  • 4
  • 16
  • 10
    You seem to be misunderstanding lazy loading as the opposite of what it actually is. Lazy loading means your data **is** automatically loaded when you access a navigation property. The **opposite** of this, eager loading, is when you get nulls for your relation properties unless you explicitly (eagerly) load them. – Asad Saeeduddin Apr 11 '14 at 09:27

3 Answers3

2

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

tdykstra
  • 5,880
  • 2
  • 23
  • 20
  • LazyLoadingEnabled is set to true because I don't want it to eager load the data. What is happening is that its eager loading the related entities when it shouldn't be. – bretcj7 May 18 '12 at 13:39
  • How do you know it is eager loading? The very act of trying to inspect a navigation property triggers the lazy loading. With lazy loading on you can't see a null navigation property if there are related entities to be loaded. – tdykstra May 18 '12 at 15:52
  • Even when viewing the object, the property without expanding it shows a Count = 1 for the DrugIdentities property. Will that still trigger eager loading by just looking at the top level class that is populated? – bretcj7 May 18 '12 at 17:09
1

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;
Servy
  • 202,030
  • 26
  • 332
  • 449
bretcj7
  • 355
  • 2
  • 4
  • 16
0

It is the very behavior of lazy loading. if you just use the property of drugs, then there will not be any sql to query DrugIdentities. if you use DrugIdentities even just watch it in debug window, then there will be sql to query DrugIdentities and each drug with a single DrugIdentities search query. You can prove the behavior through peeking the sql captured by SQL Profiler. If you want DrugIdentities to be null while querying drug, you may change the model by remove virtual key word before DrugIdentities. Then when you query drug, DrugIdentities will keep null until you load DrugIdentities to context by another query.

Roy Liu
  • 51
  • 3