我想在模型中包含一个Person实体。没有相应的表格,也没有区分符。我只是希望将其作为其他实体共享的通用基类(我想定义具有通用功能的public partial class Person
。
因此,例如,我想要:
// this class has no corresponding table in the DB
public partial abstract class Person
{
public int Id {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
public string DisplayName { get { return string.Format("{0}, {1}", LastName, FirstName); } }
}
// this corresponds to dbo.Users
public partial class User : Person
{
}
// this corresponds to dbo.Contacts
public partial class Contact : Person
{
}
因此,我创建了一个抽象实体Person,将其设置为User和Contact实体的基类,然后添加我的属性,并为dbo.Users和dbo.Contacts设置了PK Id,FirstName和LastName属性的映射。
我得到错误:
3032: Problem in mapping fragments starting at lines 2109
..... are being mapped to the same rows in table .... Mapping conditions can be used to distinguish the rows that these types are mapped to.
我该如何工作?
最佳答案
您必须在EDMX文件中定义Person
实体,并从EDMX中的User
继承Contact
和Person
。 Person
中定义的属性均不能出现在继承的实体中。然后转到映射详细信息并将继承的实体映射到相应的表。
请注意,主键现在必须对所有人都是唯一的-不仅在派生实体中。
关于.net - Entity Framework 逻辑实体错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6273498/