【问题标题】:EF lazy loading: EntityType does not declare a navigation property with the nameEF 延迟加载:EntityType 未声明名称为导航属性
【发布时间】:2012-10-12 20:22:29
【问题描述】:

我有一个类似这样的类结构:

public class Customer{
         public AgreementDetails Agreement { get; set; }
}

[ComplexType]
public class AgreementDetails{
    public int SalesPersonId { get; set; }
    [ForeignKey("SalesPersonId")]
    [NotMapped]
    public virtual SalesPerson Sales { get; set; }
}
public class SalesPerson
{
...
}

当我显示客户列表时,我正在调用 Sales 属性以显示销售人员详细信息,但属性为 null,通常对于导航属性,可以避免使用 .Include 方法,但在我的情况下它不起作用,所以查询是

List<Customer> res = db.Customers.Include(c=>c.Agreement.Sales).ToList();

例外是:

指定的包含路径无效。实体类型 “CodeFirstNamespace.Customer”未声明导航属性 名称为“协议”。

【问题讨论】:

    标签: entity-framework lazy-loading


    【解决方案1】:

    我找到了答案:复杂类型不应包含导航属性(以及外键)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-21
      相关资源
      最近更新 更多