EF6 lazy loading doesn't work while eager loading works fine

c# entity-framework-6

Question

After updating my app from EF4.1 to EF6, I started experiencing lazy loading problems. To create a fresh DbContext, I used the EF6.x DbContext Generator. Every piece of advice from this article is also put into practice.

  • I teach in a public setting.
  • not abstract or sealed
  • Have a public builder
  • Don't do it, eitherIEntityWithChangeTracker nor IEntityWithRelationships
  • Both ProxyCreationEnabled and LazyLoadingEnabled positioned to true
  • Virtual navigational properties

What I find strange is that if I include the navigation property withInclude("...") It is fueled.

a condensed version of my POCOs and database:

public partial class Ideation
{
    public Ideation()
    {

    }

    public long Id { get; set; }
    public Nullable<long> ChallengeId { get; set; }

    public virtual Challenge Challenge { get; set; }
}

public partial class Challenge
{
    public Challenge()
    {
        this.Ideations = new HashSet<Ideation>();
    }

    public long Id { get; set; }

    public virtual ICollection<Ideation> Ideations { get; set; }
}

public partial class BoxEntities : DbContext
{
    public TIBoxEntities()
        : base("name=BoxEntities")
    {

    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet<Ideation> Ideations { get; set; }
    public virtual DbSet<Challenge> Challenges { get; set; }
}

I also attempted to establishProxyCreationEnabled and LazyLoadingEnabled specifically without success. This screenshot from a debug session demonstrates that the entity is not loaded as a dynamic proxy:

Debug

How much more am I missing?

1
5
3/3/2014 6:40:07 PM

Accepted Answer

An instance where this can occur is if the entity you're attempting to loadFind is already a non-proxy object that is linked to the context. For instance:

using (var context = new MyContext())
{
    var ideation = new Ideation { Id = 1 }; // this is NOT a proxy
    context.Ideations.Attach(ideation);

    // other stuff maybe ...

    var anotherIdeation = context.Ideations.Find(1);
}

anotherIdeation will be the associated non-proxy, which is unable to perform lazy loading. Even executing a database query withvar anotherIdeation = context.Ideations.SingleOrDefault(i => i.Id == 1); because queries' default merge option isAppendOnly , meaning that if there isn't currently a connected entity with that key, the new entity wouldn't be added. So,anotherIdeation would nonetheless not be a proxy.

If the entity is already associated, you can check usingLocal prior to callingFind in theGetById method:

bool isIdeationAttached = context.Ideations.Local.Any(i => i.Id == id);
4
3/3/2014 7:00:40 PM

Popular Answer

According to @ken2k's reply, starting with EF 4, lazy loading was enabled by default for new models. It was not allowed with EF 1. It is kept off by default if you moved your model from version 1 to version 4. To enable that, the context would need to be changed.

Having stated that, you demonstrate that it is accurate through debugging. Check your usage situations in that instance. It's possible that your context was already disposed of when you went to fetch the child items. Usually, when data binding to a LINQ query, the context is set up in a Using block, and the real iteration doesn't begin until the scope of the Using block has ended.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow