避免循环依赖:MySQL/实体框架

发布于 2024-12-03 03:08:15 字数 522 浏览 0 评论 0原文

我们在处理循环依赖时遇到了麻烦,想知道是否有人可以提出解决方案。如果我们想删除一个客户端,实体框架会拒绝这样做,因为它告诉我们外键约束失败。我们的表格设置如下:

ClientAccounts

Id [PK]
Forenames
Surname
DefaultEmailId [FK, NULLABLE]

ClientEmailAddresses

Id [PK]
Description
EmailAddress
ClientId [FK, NON-NULLABLE]

因此,客户可以拥有与其关联的零个或多个电子邮件地址帐户。其中之一将是他们的默认联系电子邮件地址。

我认识到,如果我们允许 ClientEmailAddresses 表有一个空的 ClientId,它就可以正常工作;但我们不希望出现孤立电子邮件记录的情况。

We're having trouble dealing with a circular dependancy and wondered if anyone could suggest a solution. If we want to delete a client, entity framework refuses to do this because it tells us a foreign key constraint fails. Our tables are setup like this:

ClientAccounts

Id [PK]
Forenames
Surname
DefaultEmailId [FK, NULLABLE]

ClientEmailAddresses

Id [PK]
Description
EmailAddress
ClientId [FK, NON-NULLABLE]

So a Client can have zero or many email addresses associated with their account. One of which will be their default contact email address.

I recognise that if we allowed the ClientEmailAddresses table to have a null ClientId, it would work ok; but we don't want a situation where we could have orphan email records.

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

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

发布评论

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

评论(2

旧街凉风 2024-12-10 03:08:15

也许最好将“default”字段存储在表 ClientEmailAddresses 中,并从表 ClientAccounts 中删除 DefaultEmailId。我不知道你的代码结构,但有时像你这样的结构是多余的。

Maybe it's better to store field 'default' in table ClientEmailAddresses and remove DefaultEmailId from table ClientAccounts. I don't know your code structure but sometimes structure like yours is redundant.

恋你朝朝暮暮 2024-12-10 03:08:15

您是否应该首先删除 ClientEmailAddresses 中的条目?

想象一下这样的存储过程:

##  delete Client
delete from ClientEmailAddresses where ClientId = ID2delete
delete from ClientAccounts where Id = ID2delete

其中 Id2delete 是要删除的客户端 ID。

我没有尝试过,但应该可以!

Shouldn't you first delete the entries in ClientEmailAddresses ?

Imagine a stored procedure like this :

##  delete Client
delete from ClientEmailAddresses where ClientId = ID2delete
delete from ClientAccounts where Id = ID2delete

where Id2delete is the client ID to delete.

I haven't try, but should work !

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