[英]Entity Framework 6: no lazy loading on one-to-one relationship
我首先使用代码到现有的数据库,我在使用其中一个导航属性到延迟加载时遇到了一些麻烦。 即我的代码库中唯一的一对一关系。
实体看起来像这样:
public abstract class DomainObject<T>
{
/// <summary>
/// The Id of this object.
/// </summary>
[Key]
public T Id { get; set; }
}
[Table("Ordre")]
public class KundeOrdre : DomainObject<int>
{
//normal properties above
public virtual Bestilling Bestilling { get; set; }
//Various methods and other navigational properties below
}
[Table("Bestilling")]
public class Bestilling : DomainObject<int>
{
//normal properties above
public virtual KundeOrdre KundeOrdre { get; private set; }
//Various methods and other navigational properties below
}
他们流畅的映射看起来像这样:
modelBuilder.Entity<KundeOrdre>()
.HasRequired<Bestilling>(x => x.Bestilling)
.WithRequiredPrincipal(x => x.KundeOrdre)
.Map(x => x.MapKey(OrderFK));
如果我渴望加载bestilling这似乎按预期工作,但如果我尝试延迟加载它我得到一个对象,其中所有属性为null或默认值。
我在代码中发现了一个设计错误,bestilling导航属性在构造函数中被实例化。 删除该行可以在一个笔划中修复我的所有问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.