【问题标题】:nHibernate: Use one tabel in multiple many to one mappingsnHibernate:在多个多对一映射中使用一个表
【发布时间】:2014-06-23 08:58:24
【问题描述】:

我的 SellDepartment 一切正常。我可以保存并获取 PriceLines,但是当我尝试将数据保存到我的 SellEmployee 表时出现外键约束错误:

INSERT 语句与 FOREIGN KEY 约束冲突 "FK9B5C4AB8B12F319A"

我知道我可以通过创建一个 SellDepartment 和 SellEmployee 然后继承自的 SellBase 类来解决这个问题,但我真的不喜欢为此在数据库中有一个额外的表。

所以我的问题是,我可以只使用映射文件来解决这个问题吗?如果是,我做错了什么?

我有一个表格,其中包含不同其他表格的行:

**PriceLines tabel**
PriceLineId
Price
Type
SellId

**SellDepartment tabel**
SellDepartmentId
Reason
DepartmentId

**SellEmployee tabel**
SellEmployeeId
Reason
EmployeeId

我的 PriceLines 的 nHibernate 映射文件有这些多对一的映射:

<subclass name="Source1Line" discriminator-value="DepartmentLine" extends="PriceLines">
  <many-to-one name="Source1" column="SourceId" class="Source1" />
</subclass>

<subclass name="Source2Line" discriminator-value="EmployeeLine" extends="PriceLines">
  <many-to-one name="Source2" column="SourceId" class="Source2"  />
</subclass>

以及我的 SellDepartment 和 SellEmployee 的映射文件:

<set name="PriceLines" cascade="all-delete-orphan" inverse="true">
  <key column="SellId"/>
  <one-to-many class="DepartmentLine"/>
</set>

<set name="PriceLines" cascade="all-delete-orphan" inverse="true">
  <key column="SellId"/>
  <one-to-many class="EmployeeLine"/>
</set>

【问题讨论】:

  • 请注意:也许这只是我和我的问题,但您上面的简化定义并不清楚。虽然我想帮忙,但不知道这到底是怎么回事......

标签: c# nhibernate nhibernate-mapping


【解决方案1】:

您的数据库设计似乎不正确,我可以从表/映射文件中注意到的问题是,

Priceline.SellId 可以是 SellEmployeeId 或 SellDepartmentId,根据我在这里的信息,我假设它们是主键。

因此,您对 Priceline 具有 SellEmployee 和 SellDepartment 的 2 个约束,当您向 Priceline 插入一行时,约束期望在 SellEmployee 和 SellDepartment 中都有相关记录,但情况并非如此。这就是它失败的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    • 2014-06-07
    相关资源
    最近更新 更多