【问题标题】:Entity Framework 4.3 Eager Loading Zero to manyEntity Framework 4.3 Eager Loading 零到多
【发布时间】:2012-03-20 13:53:28
【问题描述】:

我有一个如下所示的数据库:

[Invoice]
-PK:ID
-...

[InvoiceDetail]
-PK:ID
-FK:Invoice_ID
-...

我的 C# 对象如下所示:

[Invoice]
public virtual IEnumerable<InvoiceDetail> InvoiceDetails { get; set; }
...

[InvoiceDetail]
...

当我尝试使用时:

MyDbContext.Invoices.Where(inv => inv.Id == 1).Include("InvoiceDetails").FirstOrDefault();
//or
MyDbContext.Invoices.Include("InvoiceDetails").Single(inv => inv.Id == 1);

我有一个例外:

指定的包含路径无效。实体类型 'DataAccessLayer.Database.Invoice' 没有声明 名为“InvoiceDetails”的导航属性。

我不明白为什么它搜索没有其模型的“DataAccessLayer.Database.Invoice”。我也不理解“导航属性”,因为我使用的是 Poco 对象(代码优先)。

有谁知道我怎么能做到这种零对多的关系?

【问题讨论】:

    标签: c# entity-framework entity-framework-4 entity-framework-4.3


    【解决方案1】:

    我想你想改用ICollection&lt;InvoiceDetail&gt;

    【讨论】:

    • +1 不错的收获。我还没有创建类,也没有意识到虚拟方法返回的是 IEnumerator 而不是 ICollection。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 2018-04-07
    相关资源
    最近更新 更多