Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Code First Lazy loading Not Working

I am using code first with EF6 but cannot seem to get lazy loading to work. Eager loading is working fine. I have the following classes:

public class Merchant : User {     ...      public virtual ICollection<MerchantLocation> MerchantLocations { get; set; } }  public class MerchantLocation : BaseEntity {     ...      public int MerchantId { get; set; }     public virtual Merchant Merchant { get; set; }        }  public class User : BaseEntity {     ... }  public class BaseEntity {     ...      public int Id { get; set; } } 

I test my lazy loading of the locations via the following code (which fails):

public void Test_Lazy_Loading() {     using (var context = new MyDbContext()) {         var merchant = context.Users.OfType<Merchant>.First();         merchant.MerchantLocations.ShouldNotBeNull(); // fails     } } 

However eager loading works fine:

public void Test_Eager_Loading() {     using (var context = new MyDbContext()) {         var merchant = context.Users.OfType<Merchant>.Include("MerchantLocations").First();         merchant.MerchantLocations.ShouldNotBeNull(); // passes     } } 

MerchantLocations is marked as public virtual so I'm not sure what the problem is. I have also added the following in my DbContext constructor:

Configuration.LazyLoadingEnabled = true; Configuration.ProxyCreationEnabled = true; 

edit: I have also noticed that the merchant object being returned in the above tests is not an EF proxy. It is a plain Merchant. I suspect that this is causing the problem.

like image 487
user1032657 Avatar asked Feb 26 '14 04:02

user1032657


People also ask

How do I enable lazy loading code first in Entity Framework?

Entity Framework : A Comprehensive Course Lazy loading means delaying the loading of related data, until you specifically request for it. When using POCO entity types, lazy loading is achieved by creating instances of derived proxy types and then overriding virtual properties to add the loading hook.

How do I fix lazy loading?

To lazy load an image, display a lightweight placeholder image, and replace with the real full-size image on scroll. There are several technical approaches to lazy loading images: Inline <img> tags, using JavaScript to populate the tag if image is in viewport. Event handlers such as scroll or resize.

Does Entity Framework support lazy loading?

Entity Framework supports three ways to load related data - eager loading, lazy loading and explicit loading.


2 Answers

I realized that the problem was that the Merchant class did not meet requirements for proxy generation. Specifically, I needed to add a protected parameterless constructor. I only had a private one.

like image 103
user1032657 Avatar answered Sep 26 '22 01:09

user1032657


Another thing that can cause lazy loading to fail is navigation properties that are not virtual. That was not the case for OP, but this question is a top Google result so it may help some.

And yet another possible cause is a mapped database column that doesn't exist. I was surprised to see that break lazy loading rather than throw a database exception.

like image 26
user2880616 Avatar answered Sep 24 '22 01:09

user2880616