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

360code
  • 45
  • 4
  • 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. – Sirwan Afifi Aug 25 '15 at 08:06
  • So how would I get lazy loading to work in the second using statement? – 360code Aug 25 '15 at 08:14
  • You can use one using statement for those queries – Sirwan Afifi Aug 25 '15 at 08: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 returned by Where(). – DevilSuichiro Aug 25 '15 at 08:25
  • Shouldn't you use db.AClasses.Include("b").where – Luc Aug 25 '15 at 08:29
  • Using `Include` causes to load data eagerly not lazily. – Taher Rahgooy Aug 25 '15 at 08:34

1 Answers1

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.

Taher Rahgooy
  • 6,528
  • 3
  • 19
  • 30