MySQL +实体框架:无法删除对象
我试图通过实体框架(MySQL/连接器)删除具有外键关系的对象。
客户帐户 >>> ClientEmailAddresses
foreach (ClientAccount client in recsClientStore.Deleted) {
ClientAccount stub = new ClientAccount();
stub.Id = client.Id;
this.DBContext.AttachTo("ClientAccounts", stub);
stub.FkClientEmailAddresses.Clear();
this.DBContext.DeleteObject(stub);
}
this.DBContext.SaveChanges();
..但是当我这样做时,出现以下错误:
操作失败:无法更改关系,因为 一个或多个外键属性不可为 null。当一个 对关系进行更改时,相关的外键属性是 设置为空值。如果外键不支持空值, 必须定义一个新的关系,外键属性必须是 分配另一个非空值,或者不相关的对象必须是 已删除。
我不太确定这会让我做什么。我必须先删除 EmailAddress 对象吗?我们对打开级联持谨慎态度,但看起来越来越像是清理外键所必需的。
I'm trying to delete an object that has foreign key relationships via the Entity Framework (MySQL/Connector).
ClientAccount >>> ClientEmailAddresses
foreach (ClientAccount client in recsClientStore.Deleted) {
ClientAccount stub = new ClientAccount();
stub.Id = client.Id;
this.DBContext.AttachTo("ClientAccounts", stub);
stub.FkClientEmailAddresses.Clear();
this.DBContext.DeleteObject(stub);
}
this.DBContext.SaveChanges();
.. but when I do this I get the following error:
The operation failed: The relationship could not be changed because
one or more of the foreign-key properties is non-nullable. When a
change is made to a relationship, the related foreign-key property is
set to a null value. If the foreign-key does not support null values,
a new relationship must be defined, the foreign-key property must be
assigned another non-null value, or the unrelated object must be
deleted.
I'm not really sure where this leaves me. Do I have to delete the EmailAddress object first? We're wary of turning on cascade but it's looking more and more like this is required to tidy up foreign keys.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是:
不会删除关系。它只是将相关实体中的FK设置为null。如果您希望它们真正删除,则必须:
Remove
来删除它们Clear
按预期工作The problem is that this:
doesn't delete relations. It only set FK in related entities to null. If you want them really delete you must either:
Remove
on their object setClear
work as expected