实体框架 .Remove() 与 .DeleteObject() [英] Entity Framework .Remove() vs. .DeleteObject()

查看:24
本文介绍了实体框架 .Remove() 与 .DeleteObject()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以通过以下两种方法使用 EF 从数据库中删除项目.

You can remove an item from a database using EF by using the following two methods.

ObjectContext.DeleteObject 方法

第一个在 EntityCollection 上,第二个在 ObjectContext 上.

The first is on the EntityCollection and the second on the ObjectContext.

应该什么时候使用?

一个比另一个更受欢迎吗?

Is one prefered over the other?

Remove() 返回一个 bool 并且 DeleteObject() 返回 void.

Remove() returns a bool and DeleteObject() returns void.

推荐答案

使用这两种方法都可以从数据库中删除项目"通常是不正确的.准确的说是这样的:

It's not generally correct that you can "remove an item from a database" with both methods. To be precise it is like so:

  • ObjectContext.DeleteObject(entity) 在上下文中将实体标记为 Deleted.(之后是 EntityStateDeleted.)如果之后调用 SaveChanges,EF 会向数据库.如果没有违反数据库中的引用约束,实体将被删除,否则抛出异常.

  • ObjectContext.DeleteObject(entity) marks the entity as Deleted in the context. (It's EntityState is Deleted after that.) If you call SaveChanges afterwards EF sends a SQL DELETE statement to the database. If no referential constraints in the database are violated the entity will be deleted, otherwise an exception is thrown.

EntityCollection.Remove(childEntity)parent 和childEntity 之间的关系标记为Deleted.如果 childEntity 本身被从数据库中删除,当你调用 SaveChanges 时究竟会发生什么取决于两者之间的关系类型:

EntityCollection.Remove(childEntity) marks the relationship between parent and childEntity as Deleted. If the childEntity itself is deleted from the database and what exactly happens when you call SaveChanges depends on the kind of relationship between the two:

  • 如果关系是可选,即数据库中从子到父的外键允许NULL值,这个外键将被设置为空,如果你调用 SaveChanges 这个 NULLchildEntity 将被写入数据库(即两者之间的关系被删除).这发生在 SQL UPDATE 语句中.不会出现 DELETE 语句.

  • If the relationship is optional, i.e. the foreign key that refers from the child to the parent in the database allows NULL values, this foreign will be set to null and if you call SaveChanges this NULL value for the childEntity will be written to the database (i.e. the relationship between the two is removed). This happens with a SQL UPDATE statement. No DELETE statement occurs.

如果关系是必需的(FK 不允许 NULL 值)并且关系不能识别(这意味着外键不是子键(复合)主键的一部分)您必须将子键添加到另一个父键或必须显式删除子键(然后使用 DeleteObject).如果您不执行任何这些操作,则会违反引用约束,并且当您调用 SaveChanges 时,EF 将抛出异常 - 臭名昭著的 "无法更改关系,因为一个或多个外国-关键属性不可为空"异常或类似.

If the relationship is required (the FK doesn't allow NULL values) and the relationship is not identifying (which means that the foreign key is not part of the child's (composite) primary key) you have to either add the child to another parent or you have to explicitly delete the child (with DeleteObject then). If you don't do any of these a referential constraint is violated and EF will throw an exception when you call SaveChanges - the infamous "The relationship could not be changed because one or more of the foreign-key properties is non-nullable" exception or similar.

如果关系是识别(它必须必需然后因为主键的任何部分不能是NULL)EF将也将 childEntity 标记为 Deleted.如果您调用 SaveChanges,一条 SQL DELETE 语句将被发送到数据库.如果没有违反数据库中的其他引用约束,该实体将被删除,否则抛出异常.

If the relationship is identifying (it's necessarily required then because any part of the primary key cannot be NULL) EF will mark the childEntity as Deleted as well. If you call SaveChanges a SQL DELETE statement will be sent to the database. If no other referential constraints in the database are violated the entity will be deleted, otherwise an exception is thrown.

我实际上对 MSDN 页面上的备注部分有些困惑 您已链接,因为它说:如果关系具有参照完整性约束,则对依赖对象调用 Remove 方法会将关系和依赖对象标记为删除.".这对我来说似乎不准确甚至是错误的,因为上述所有三种情况都有一个参照完整性约束",但只有在最后一种情况下,子项实际上被删除了.(除非他们的意思是依赖对象"是一个参与识别关系的对象,但这将是一个不寻常的术语.)

I am actually a bit confused about the Remarks section on the MSDN page you have linked because it says: "If the relationship has a referential integrity constraint, calling the Remove method on a dependent object marks both the relationship and the dependent object for deletion.". This seems unprecise or even wrong to me because all three cases above have a "referential integrity constraint" but only in the last case the child is in fact deleted. (Unless they mean with "dependent object" an object that participates in an identifying relationship which would be an unusual terminology though.)

这篇关于实体框架 .Remove() 与 .DeleteObject()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆