Entity framework EF lazy loading:EntityType未声明名称为的导航属性
我有一个类似这样的类结构:Entity framework EF lazy loading:EntityType未声明名称为的导航属性,entity-framework,lazy-loading,Entity Framework,Lazy Loading,我有一个类似这样的类结构: public class Customer{ public AgreementDetails Agreement { get; set; } } [ComplexType] public class AgreementDetails{ public int SalesPersonId { get; set; } [ForeignKey("SalesPersonId")] [NotMapped] public virt
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属性来取消显示Sales person详细信息,但属性为null,通常用于导航属性,这可以避免使用。Include方法,但在我的情况下它不起作用,因此查询为空
List<Customer> res = db.Customers.Include(c=>c.Agreement.Sales).ToList();
List res=db.Customers.Include(c=>c.Agreement.Sales.ToList();
例外情况是:
指定的包含路径无效。实体类型
“CodeFirstNamespace.Customer”未声明导航属性
名称为“协议”
我找到了答案:复杂类型不应该包含导航属性(以及外键)。