EF4 中的“共享”多对多关系列

发布于 2024-11-09 08:50:35 字数 1354 浏览 0 评论 0原文

免责声明:严格来说,我这里的关系不是多对多关系,但考虑到它使用关联表,我认为最好稍微不那么准确,以便更好地了解我的内容我在做。

我的数据库中有相当于以下三个表的内容:

Customer
---------
CustomerID PK
...

CustomerAddress
---------
CustomerID PK, FK -> Customer
AddressNo  PK
... address columns ...

CustomerPrimaryAddress
--------------
CustomerID PK, FK -> Customer
AddressNo      FK -> CustomerAddress (with CustomerID, so CustomerID 
                                      participates in both relationships)

如果不明显,这里的目的是允许每个客户有多个地址,同时将每个客户最多指定一个地址作为“主要”。我使用关联表来避免在 Customer 上放置可为空的 PrimaryAddressNumber 列,然后创建从 CustomerCustomerAddress 的外键

这一切都很好,但 EF 随后将 CustomerPrimaryAddress 实体放入我的模型中。由于它的唯一目的是充当关联表,因此我不需要在代码中表示该表。我从概念模型中删除了 CustomerPrimaryAddress 表,然后在 CustomerCustomerAddress 之间创建了一个关联,如下所示:

Table          Customer   CustomerAddress
Multiplicity   1          0..1

然后我映射该关联以使用来自存储模型的 CustomerPrimaryAddress 表,以及所有映射的列都很好,至少我是这么认为的。

我的问题是,现在 EF 抱怨 CustomerPrimaryAddress 中的 CustomerID 被映射到我关联中的两个位置,因为它同时映射到 Customer客户地址

有什么办法解决这个问题吗?我不想向 Customer 添加可为空的列来表示主地址编号,因为从 DBA 的角度来看,这不仅不是一个令人愉快的选择,EF 还会抱怨存在循环关系,而我必须打破代码中的插入。

Disclaimer: Strictly speaking, what I have here is not a many-to-many relationship, but given that it's using an associative table, I figured it would be better to be slightly less accurate in order to give a better idea of what I'm doing.

I have the equivalent of the following three tables in my database:

Customer
---------
CustomerID PK
...

CustomerAddress
---------
CustomerID PK, FK -> Customer
AddressNo  PK
... address columns ...

CustomerPrimaryAddress
--------------
CustomerID PK, FK -> Customer
AddressNo      FK -> CustomerAddress (with CustomerID, so CustomerID 
                                      participates in both relationships)

If it's not obvious, the intent here is to allow for multiple addresses per customer, while designating at most one address per customer as "primary". I'm using an associative table to avoid placing a nullable PrimaryAddressNumber column on Customer, then creating a foreign key from Customer to CustomerAddress.

This is all well and good, but EF then places the CustomerPrimaryAddress entity in my model. Since its one and only purpose is to serve as an associative table, I have no need to represent this table in code. I removed the CustomerPrimaryAddress table from the conceptual model, then created an association between Customer and CustomerAddress like so:

Table          Customer   CustomerAddress
Multiplicity   1          0..1

I then mapped the association to use the CustomerPrimaryAddress table from the storage model, and all of the columns mapped just fine, or so I thought.

My issue is that now EF is complaining that CustomerID in CustomerPrimaryAddress is being mapped to two locations in my association, as it's mapped to both Customer and CustomerAddress.

Is there any way around this? I would prefer not to add a nullable column to Customer to represent the primary address number, as not only is this not a pleasant option from a DBA perspective, EF will also complain about having a cyclical relationship and I'll have to break inserts up in the code.

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

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

发布评论

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

评论(1

败给现实 2024-11-16 08:50:35

在这里大声思考:

Customer
---------
CustomerID PK
...

CustomerAddress
---------
AddressNo  PK
CustomerID FK -> Customer, non-nullable
... address columns ...

CustomerPrimaryAddress
--------------
CustomerID PK, FK -> Customer
AddressNo      FK -> CustomerAddress 

似乎应该得到正确的基数,但我可能错过了一些东西。

Thinking out loud here:

Customer
---------
CustomerID PK
...

CustomerAddress
---------
AddressNo  PK
CustomerID FK -> Customer, non-nullable
... address columns ...

CustomerPrimaryAddress
--------------
CustomerID PK, FK -> Customer
AddressNo      FK -> CustomerAddress 

This seems like it should get the cardinalities right, but I may have missed something.

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