gpt4 book ai didi

c# - Entity Framework 6 : no lazy loading on one-to-one relationship

转载 作者:行者123 更新时间:2023-11-30 16:05:50 24 4
gpt4 key购买 nike

我首先对现有数据库使用代码,但在将我的导航属性之一延迟加载时遇到了一些问题。即我的代码库中唯一的一对一关系。

实体看起来像这样:

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 导航属性。删除该行一次性解决了我的所有问题。

关于c# - Entity Framework 6 : no lazy loading on one-to-one relationship,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33075235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com