簡體   English   中英

實體框架.include拋出NullReferenceException

[英]Entity Framework .Include throwing NullReferenceException

我有一個非常基本的EF設置,當嘗試使用.include填充導航屬性時,它會拋出一個奇怪的錯誤。 這是實體模型:

public class LineGroup
{
    public int ID { get; set; }
    public string Name { get; set; }
    public bool IsActive { get; set; }  
    public ICollection<LineGroupMember> LineGroupMembers { get; set; }
}

public class LineGroupMember
{
    public int ID { get; set; }
    public int Extension { get; set; }
    public string Name { get; set; }
    public int Permissions { get; set; }
    public bool IsLoggedIn { get; set; }

    public int LineGroupID { get; set; }

    internal LineGroup LineGroup { get; set; }
}

我通過注入的數據庫上下文使用它們,並且可以在不使用導航屬性的情況下查詢每個屬性。 我還可以查詢LineGroups並包括LineGroupMembers屬性,就像這樣:

var LineGroups = _context.LineGroups.Include(l => l.LineGroupMembers).ToList();

這會將所有線路組加載到一個列表中,該列表具有每個線路組正確工作的“ LineGroupMembers”集合。 但是,如果我嘗試

var lineGroupMembers = _context.LineGroupMembers.Include(m => m.LineGroup).ToList();

我收到“ NullReferenceException”,沒有任何有用的詳細信息。 有什么想法為什么導航屬性會以一種方式而不是另一種方式起作用? 任何一個數據庫表中都沒有空值...

public您的導航屬性

public LineGroup LineGroup { get; set; }

如果它是internal ,則默認情況下不會被EF接收。 您還可以添加顯式的流利映射,以強制EF也識別它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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