【问题标题】:Entity Framework 4: Inheritance (table per type) - Derived table has a composite PK实体框架 4:继承(每种类型的表) - 派生表具有复合 PK
【发布时间】:2011-04-19 17:35:15
【问题描述】:

如果派生表具有复合主键,是否可以在 Entity Framework 4 中实现“每个类型的表”继承?

这是我的餐桌设置:

TABLE: ConfigurationKey (Base Entity)
PK: Id

TABLE: ConfigurationKey_Device (Derived Entity)
PK: ConfigurationKeyId (FK to ConfigurationKey.Id)
PK: DeviceId (FK to Device.Id)

不管怎样,ConfigurationKey 将是抽象的,其他类型将从 ConfigurationKey 派生。

使用 EF 设计器,我有:

  • 添加了继承规则
  • 从 ConfigurationKey_Device 中删除了 ConfigurationKeyId
  • 删除了将 ConfigurationKey_Device 链接到 ConfigurationKey 的 FK
  • 更新了 ConfigurationKey_Device.ConfigurationKeyId 列到继承的 Id 属性的映射。

我现在得到的错误是:

错误 3003:映射问题 从第 xxx 行开始的片段:所有 关键属性(ConfigurationKeys.Id) EntitySet ConfigurationKeys 必须映射到所有键 特性 (ConfigurationKey_Device.ConfigurationKeyId, ConfigurationKey_Device.DeviceId) 的 表 ConfigurationKey_Device。

谢谢, 克里斯

【问题讨论】:

  • EF 最好不要使用复合 PK。

标签: .net entity-framework inheritance


【解决方案1】:

错误消息回答了您的问题本身。
您实际上是在尝试从具有一列键的实体和具有两列键的实体继承。
似乎更好的解决方案是使用包含公共属性集的 ComplexType,或者使用实现必要功能的公共接口。

【讨论】:

  • 是的,我试图做的事情没有意义。我认为应该是派生实体实际上更像是一个连接表。感谢您的反馈。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多