簡體   English   中英

實體框架6:在一對一關系上沒有延遲加載

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM