实体框架.Remove()与.DeleteObject() [英] Entity Framework .Remove() vs. .DeleteObject()
问题描述
您可以使用以下两种方法从EF中删除项目:
第一个是在 EntityCollection
上,第二个在 ObjectContext
。
应该何时使用?
是否优先于另一个?
Remove()
返回一个 bool
和 DeleteObject()
返回 void
。
通常情况下,您可以从数据库中删除项目两种方法。确切地说,就像这样:
-
ObjectContext.DeleteObject(entity)
在上下文中将实体标记为已删除
。 (它是EntityState
是之后删除
)如果调用SaveChanges
之后,EF向数据库发送SQLDELETE
语句。如果数据库中没有引用限制被违反,则实体将被删除,否则将抛出异常。 -
EntityCollection.Remove(childEntity )
标记父与childEntity
之间的关系为已删除
。如果childEntity
本身从数据库中删除,当您调用SaveChanges
时会发生什么,这取决于这两个:
-
如果关系是可选,即从小孩引用的外键对数据库中的父进程允许
NULL
值,这个外部设置为null,如果调用SaveChanges
code> NULLchildEntity
的值将被写入数据库(即两者之间的关系被删除)。这发生在SQLUPDATE
语句。否DELETE
语句发生。 -
如果关系需要 FK不允许
NULL
值),并且关系不能识别(这意味着外键不是孩子的(复合)主键),您必须将该子项添加到另一个父项,或者您必须显式删除该子(然后使用DeleteObject
)。如果您不执行任何这些参数约束违反,并且当您调用SaveChanges
- 臭名昭着的关系不能改变,因为一个或更多的外键属性不可为空异常或类似。 -
如果关系为识别(它必须需要,因为主键的任何部分不能是
NULL
)EF将标记childEntity
asDeleted
以及。如果调用SaveChanges
,SQLDELETE
语句将被发送到数据库。如果数据库中没有其他引用约束被违反,则实体将被删除,否则抛出异常。
-
You can remove an item from a database using EF by using the following two methods.
The first is on the EntityCollection
and the second on the ObjectContext
.
When should each be used?
Is one prefered over the other?
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)
marks the entity asDeleted
in the context. (It'sEntityState
isDeleted
after that.) If you callSaveChanges
afterwards EF sends a SQLDELETE
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)
marks the relationship between parent andchildEntity
asDeleted
. If thechildEntity
itself is deleted from the database and what exactly happens when you callSaveChanges
depends on the kind of relationship between the two: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 callSaveChanges
thisNULL
value for thechildEntity
will be written to the database (i.e. the relationship between the two is removed). This happens with a SQLUPDATE
statement. NoDELETE
statement occurs.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 (withDeleteObject
then). If you don't do any of these a referential constraint is violated and EF will throw an exception when you callSaveChanges
- the infamous "The relationship could not be changed because one or more of the foreign-key properties is non-nullable" exception or similar.If the relationship is identifying (it's necessarily required then because any part of the primary key cannot be
NULL
) EF will mark thechildEntity
asDeleted
as well. If you callSaveChanges
a SQLDELETE
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.
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屋!