I have the following Entities
public class A
{
public int AId {get; set;}
}
public class B
{
public int BId {get; set;}
public virtual A Child1 {get; set;}
public virtual A Child2 {get; set;}
}
with the following configuration
modelBuilder.Entity<B>()
.HasRequired(x => x.Child1)
.WithMany()
.Map(x => x.MapKey("Child1Id"))
modelBuilder.Entity<B>()
.HasRequired(x => x.Child2)
.WithMany()
.Map(x => x.MapKey("Child2Id"))
For some reason Entity Framework does not lazy load the Child1 or Child2 properties when requesting an Entity of object B. That is
var b1 = context.Bs.FirstOrDefault();
Assert.IsNull(b.Child1) // true
Assert.IsNull(b.Child2) // true
but it works if i explcitly loads them.
var b2 = context.Bs.Include(x => x.Child1).Include(x => x.Child2).FirstOrDefault();
Assert.NotNull(b2.Child1) // true
Assert.NotNull(b2.Child2) // true
Does anyone know why the properties does not get lazy loaded?
EDIT
It seems that
context.Bs.FirstOrDefault()
returns the entity itself and not a proxy type. This indicates that the property ProxyCreationEnabled is false but I have double checked it and it is set to true.
EDIT 2
Ok, finally found the problem. I had set Bs constructor to private which of course makes it impossible to extend the object with a proxy class. And hence, the navigation properties where set to null.