【发布时间】:2010-10-06 17:51:22
【问题描述】:
删除 EF 实体的所有集合项的正确方法是什么?在下面的代码中,DocumentItems 是文档的相关文档项的集合。此代码在 Clear() 上继续,但在 SaveChanges() 上失败,因为相关项目通过 FK 连接到它们的文档,并且 FK 是强制性的。所以我猜想它们在 Clear() 之后没有外键就一直漂浮在空中。
我是通过对每个项目调用 Remove() 的集合进行 foreach 循环来解决这个问题,还是有其他方法?
// remove existing document items to prepare for refreshing them
existing.DocumentItems.Clear();
// adds new Document Items
PrepareInvoice(existing, collection);
_repository.SaveChanges();
【问题讨论】:
标签: entity-framework entity-framework-4