1

I have tried to implement lazy and eager loading in EF. Everything seems fine, but the .Include() method is available in both cases and returns the same results irrespective of lazy loading false or true.

I have 2 tables categories and product. Category can have multiple products and related using foreign key.

When I load categories using lazy loading by using .Include("Products"), it loads all products related to categories.

Same behavior is shown by eager loading.

var results = objDB.Categories.Include("Products").ToList(); // Enabled lazy load

var results = objDB.Categories.Include("Products").ToList(); // Disabled lazy load

Above both lines have same result. Please clarify this confusion.

.Include should not be available in case of lazy loading = true.

Please give your valuable opinion. Thanks in advance.

3 Answers 3

3

When you're explicitly using Include(), you're preforming an Eager Loading. Obviously, disabling/enabling Lazy Loading has no effect.

The difference will be reflected when you'll omit the Include and try to access the Products navigation-property in some of your Category instances. When Lazy-Loading is enabled, EF will load it from the database. When it's disabled, you'll get null.

Sign up to request clarification or add additional context in comments.

1 Comment

Okay @haim, Does it means that .Include() method will decide what loading we will use. If we used .include() in either case it wil load Eagerly and if we will not use .Include() only then it depends on LazyLoadingEnabed=True /False.
1

The difference will be that if you have lazy loading on any entities which are not loaded by the original query will be accessable transparently, and when accessed will trigger an additional query. For example

var results = objDB.Categories.Include("Products").ToList(); // Enabled lazy load
var thing = results.First().Products.First().AnotherEntity;//This will trigger another query and thing will get a value

var results = objDB.Categories.Include("Products").ToList(); // Disabled lazy load
var thing = results.First().Products.First().AnotherEntity;//Thing will be null as its not included in the original result set

FWIW I think most people using lazy loading are causing themselves problems. The only place I would use lazy loading is in a forms/wpf application where people are essentially browsing data in the database and you want to fetch more data as the user clicks things. I don't think there's ever a case for lazy loading in a web application, you are better to be explicit about the data you want from the DB.

Comments

0

Include and ToList are both considered eager methods, so you wouldn't be invoking lazy loading. It's only when you try to access the property of a related entity would the lazy loading feature kick in.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.