NHibernate/SQL Server 多个外键到同一个表...可能吗?

发布于 2024-09-15 23:22:54 字数 991 浏览 1 评论 0原文

我有一个 SalesOrder 表和一个单独的 Address 表。销售订单有两个地址 - 从而避免使用列表,即交货地址和发票地址。这就是它们在 SalesOrder 映射文件中的映射方式:

<many-to-one name="DeliveryAddress" class="Address" column="`DeliveryAddressGUID`" />
<many-to-one name="InvoiceAddress" class="Address" column="`InvoiceAddressGUID`" />

实际上是否可能存在这种关系 - 同一个表中的两个单独字段引用另一个字段。如果我删除“多对一”关联之一,我可以将 Address 添加到表中,否则,如果我保留这两个关联,则会出现以下异常:

"The UPDATE statement conflicted with the FOREIGN KEY constraint \"FK67C9F3E2FBD32E03\". The conflict occurred in database \"Dispatch\", table \"dbo.Address\", column 'GUID'.\r\nThe statement has been terminated."

我做错了什么吗?

谢谢

编辑 这些是两个表的相关部分:

SalesOrder
------------
Guid
DeiveryAddressGuid
InvoiceAddressGuid
...

Address
------------
Guid
HouseNameOrNumber
AddressLine1
AddressLine2
...

这个错误真的很奇怪,因为它不允许我在两个外键都处于活动状态的情况下简单地将地址添加到地址表中(这是在 NHibernate 中,我无法直接使用 SQL Server 进行测试)。

I have a SalesOrder table and a separate Address table. The SalesOrder has two addresses - thus avoiding use of a list, there are the Delivery and Invoice address. This is how they have been mapped in the SalesOrder mapping file:

<many-to-one name="DeliveryAddress" class="Address" column="`DeliveryAddressGUID`" />
<many-to-one name="InvoiceAddress" class="Address" column="`InvoiceAddressGUID`" />

Is it actually possible to have this relationship - where two separate fields in the same table reference another. If I remove one of the "many-to-one" associations, I can add Address to the table, otherwise if I keep both associations I get the following exception:

"The UPDATE statement conflicted with the FOREIGN KEY constraint \"FK67C9F3E2FBD32E03\". The conflict occurred in database \"Dispatch\", table \"dbo.Address\", column 'GUID'.\r\nThe statement has been terminated."

Am I doing something wrong?

Thanks

Edit
These are the relevant parts of the two tables:

SalesOrder
------------
Guid
DeiveryAddressGuid
InvoiceAddressGuid
...

Address
------------
Guid
HouseNameOrNumber
AddressLine1
AddressLine2
...

The error is really strange since it won't let me simply add an address to the Address table by itself with both Foreign Keys active (this is in NHibernate, I've not been able to test this directly with SQL Server).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帝王念 2024-09-22 23:22:54

您是否检查以确保这两个条目都位于父表中?

did you check to make sure both of those entries where in the parent table?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文