0

This is my first time using EF. As such I may have missed something simple that is preventing lazy loading of my BClass. When I load AClass the 'B' property is null. I would have expected it to be populated as persisted.

For my example I have two simple classes:

public class AClass
{
    public AClass()
    {
        Id = Guid.NewGuid();
        B = new BClass();
    }
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public Guid Id { get; set; }
    public string Name { get; set; }
    public virtual BClass B { get; set; }
}

public class BClass
{
    public BClass()
    {
        Id = Guid.NewGuid();
    }
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public Guid Id { get; set; }
    public string Description { get; set; }
}

In the 'Context' class:

public DbSet<AClass> AClasses { get; set; }
public DbSet<BClass> BClasses { get; set; }

Below is a simple test. I was expecting x.B would be loaded. Instead it is null?

using (var db = new TestContext())
{
    AClass a = new AClass();
    a.Name = "AClass";
    a.B.Description = "BClass Description Goes Here!";
    db.AClasses.Add(a);
    db.SaveChanges(); // Works. Confirmed both a and a.B are persisted to the database
}

using (var db = new TestContext())
{
    AClass x = db.AClasses.Where(a => a.Name == "AClass").FirstOrDefault();
    System.Console.WriteLine(x.Name);
    System.Console.WriteLine(x.B.Description); // x.B is null. Expected it to load and the .Description property to be "BClass Description Goes Here!"
}

Changes were persisted to the database correctly. I can't post the screen shot yet.

From AClass table:

Id  Name    B_Id
B7937E1B-9CC0-4318-B179-0D54B23B6CDA    AClass  560D066B-4848-454D-B92C-F6AE4232057E

From BClass table:

Id  Description
560D066B-4848-454D-B92C-F6AE4232057E    BClass Description Goes Here!

Entity Framework Version: 6.0.0.0

6
  • It will work only when entity object is attached to some TestContext instance when the first using statement is done context is disposed, the entity is detached, and your lazy loading request cannot be performed. Commented Aug 25, 2015 at 8:06
  • So how would I get lazy loading to work in the second using statement? Commented Aug 25, 2015 at 8:14
  • You can use one using statement for those queries Commented Aug 25, 2015 at 8:16
  • AClass x = db.AClasses.Where(a => a.Name == "AClass").FirstOrDefault(); this is the Problem. After FirstOrDefault(), it doesnt enable lazy loading anymore because your value is materialized already. use .Include() extension method or work on the IQueryable<T> returned by Where(). Commented Aug 25, 2015 at 8:25
  • Shouldn't you use db.AClasses.Include("b").where Commented Aug 25, 2015 at 8:29

1 Answer 1

1

Remove B initialization form class AClass:

public AClass()
{
    Id = Guid.NewGuid();
    //B = new BClass();
}

The initialization causes the entity framework think that you assigned new value to B property of AClass and does not assign it with a proxy.

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

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.