实体框架:父和子主键具有相同名称的继承

时间:2012-07-10 14:12:51

标签: c# entity-framework inheritance

我正在尝试在两个表之间进行实体框架继承。

父母:

ParentTable
  Id: int primary key
  CustomAttribute: int

孩子:

ChilTable
  Id: int primary key (not the same one as the parent, Child specific Id)
  TCId: int foreign key to parent
  SomeInformation: String

由于某些原因,我想继续将“Id”命名为ParentTable和ChildTable的主键。这不应该打扰EntityFramwork,因为我为子表创建了另一个名为“CId”的自定义属性:

enter image description here

Child1表映射如下:

enter image description here

但是当我“验证”模型时,VS2010说......:

Error 3002: Problem in mapping fragments starting at line 103:Potential runtime violation of table Child1's keys (Child1.Id): Columns (Child1.Id) are mapped to EntitySet Parents's properties (Parents.CId) on the conceptual side but they do not form the EntitySet's key properties (Parents.Id).

基本上,我理解实体框架在我们将表的主键映射到不是实体的键的属性但是我们应该如何使用继承这一事实时才会发现问题?

只有在“子”表中没有主键时才允许继承?我应该将我的主键作为“简单键”吗?

提前致谢...

1 个答案:

答案 0 :(得分:1)

EF希望孩子的Id PK也是父母的FK。所以Child.Id既是PK也是FK。