避免循环依赖:MySQL/实体框架
我们在处理循环依赖时遇到了麻烦,想知道是否有人可以提出解决方案。如果我们想删除一个客户端,实体框架会拒绝这样做,因为它告诉我们外键约束失败。我们的表格设置如下:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许最好将“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.
您是否应该首先删除 ClientEmailAddresses 中的条目?
想象一下这样的存储过程:
其中 Id2delete 是要删除的客户端 ID。
我没有尝试过,但应该可以!
Shouldn't you first delete the entries in ClientEmailAddresses ?
Imagine a stored procedure like this :
where Id2delete is the client ID to delete.
I haven't try, but should work !