说我有2张桌子:
Customer
Id int
Name varchar
TypeId int
CustomerType
Id int
Type varchar
TypeId是CustomerType的Id的外键。现在我如何加载客户作为EF的实体:
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public int TypeId { get; set; }
[ForeignKey("TypeId")]
public string Type { get; set; }
}
Type将是CustomerType的Type属性。我在EF5中使用代码优先。它目前无效,我一直收到The navigation property X is not a declared property on type..
例外。
答案 0 :(得分:0)
您无法将属性映射到单独的表。只有实体可以映射到表。作为解决方法,您可以创建属性,其类型为CustomerType
,将映射到第二个表,并将Type
属性标记为未映射:
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public int TypeId { get; set; }
[ForeignKey("TypeId")]
protected CustomerType CustomerType { get; set; }
[NotMapped]
public string Type
{
get { return CustomerType.Type; }
set { CustomerType.Type = value; }
}
}