实体框架上的删除级联
我在删除 Entity Framework 4.1 中的相关行时遇到问题。我有具有关系的表
Book 1<--->* BookFormats
我已设置删除级联:
ALTER TABLE [dbo].[BookFormats] WITH CHECK ADD CONSTRAINT [FK_BookFormats_Book]
FOREIGN KEY([BookID]) REFERENCES [dbo].[Book] ([BookID]) on delete cascade
EDMX 属性
然后,我想删除与我的 Book
对象相关的所有 BokFormats
项目:
var originalBook = m.db.Book.First(x => x.BookID == bookId);
originalBook.BookFormats.Clear();
m.db.SaveChanges();
但是,我收到错误:
操作失败:无法更改关系,因为 一个或多个外键属性不可为 null。当一个 对关系进行更改时,相关的外键属性是 设置为空值。如果外键不支持空值, 必须定义一个新的关系,外键属性必须是 分配另一个非空值,或者不相关的对象必须是 已删除。
我对如何删除这些对象没有想法。有什么想法吗?
I have problem with deleting related rows in Entity Framework 4.1. I have tables with relations
Book 1<--->* BookFormats
I have set the on delete cascade:
ALTER TABLE [dbo].[BookFormats] WITH CHECK ADD CONSTRAINT [FK_BookFormats_Book]
FOREIGN KEY([BookID]) REFERENCES [dbo].[Book] ([BookID]) on delete cascade
The EDMX property
Then, I want to remove the all BokFormats
items related to my Book
object:
var originalBook = m.db.Book.First(x => x.BookID == bookId);
originalBook.BookFormats.Clear();
m.db.SaveChanges();
But, I get the 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 ran out of ideas on how to delete these objects. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用RemoveRange:
但这适用于EF 6.0
You can use RemoveRange :
But this is for EF 6.0
级联删除的概念如下:
当您从数据库中删除
Book
时,SQL Server 将会为您删除所有相关的BookFormats
(请注意,无论如何删除都没有关系)Book
将通过 EF 或原始 SQL 启动)。因此,它与您的任务无关:“我想删除与我的Book
相关的所有BookFormats
”。为了实现它,你需要这样的东西:Cascade deletions concept is as follows:
When you delete
Book
from the DB all relatedBookFormats
will be deleted for you by SQL Server (please note that it doesn't matter how deletion ofBook
will be initiated via EF or raw SQL). Thus it has nothing to do with your task: "I want to delete allBookFormats
related to myBook
". To accomplish it you need something like this:您并不是从数据库中删除
BookFormats
,而是删除关系,从而孤立您的BookFormats
并将BookID
列设置为 NULL。您在数据库上进行的删除级联表示当我删除
Book时,然后删除所有具有
BookID的
BookFormats与我的相同。
您不是在删除这本书,而是从书籍
中删除格式。你应该有这样的东西,而不是
originalBook.BookFormats.Clear()
......它应该是类似的东西。我不记得 EF 如何在 EDMX 中构造删除方法。
You are not deleting the
BookFormats
from the database, but you are removing the relationship, thus orpahning yourBookFormats
and setting theBookID
column to NULL. The delete cascade you have put on the database saysWhen I delete the
Book, then delete all of the
BookFormatsthat have a
BookIDequal to mine.
You are not deleting the book you are deleting the formats from theBook
.Instead of
originalBook.BookFormats.Clear()
you should have something like this...It should be something along those lines. I don't have it right in front of me to remember how EF constructs the delete method in the EDMX.
我已经在 EF 6.1.3 中对其进行了测试,应该可以正常工作:
I've tested it in EF 6.1.3 and this should work fine:
我使用 EF6,这有效。
I use EF6 and this works.