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

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

问题描述

您可以使用以下两种方法从EF中删除项目:





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



应该何时使用?



是否优先于另一个?



Remove()返回一个 bool DeleteObject()返回 void

解决方案

通常情况下,您可以从数据库中删除项目两种方法。确切地说,就像这样:




  • ObjectContext.DeleteObject(entity)在上下文中将实体标记为已删除 。 (它是 EntityState 之后删除)如果调用 SaveChanges 之后,EF向数据库发送SQL DELETE 语句。如果数据库中没有引用限制被违反,则实体将被删除,否则将抛出异常。


  • EntityCollection.Remove(childEntity )标记父与 childEntity 之间的关系为已删除 。如果 childEntity 本身从数据库中删除,当您调用 SaveChanges 时会发生什么,这取决于这两个:




    • 如果关系是可选,即从小孩引用的外键对数据库中的父进程允许 NULL 值,这个外部设置为null,如果调用 SaveChanges code> NULL childEntity 的值将被写入数据库(即两者之间的关系被删除)。这发生在SQL UPDATE 语句。否 DELETE 语句发生。


    • 如果关系需要 FK不允许 NULL 值),并且关系不能识别(这意味着外键不是孩子的(复合)主键),您必须将该子项添加到另一个父项,或者您必须显式删除该子(然后使用 DeleteObject )。如果您不执行任何这些参数约束违反,并且当您调用 SaveChanges - 臭名昭着的关系不能改变,因为一个或更多的外键属性不可为空异常或类似。


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



对于,我实际上有点困惑,您链接的MSDN页面上的备注部分,因为它说:如果关系具有参照完整性约束,则在依赖对象上调用Remove方法标记关系和依赖对象进行删除。 / EM>。这似乎是不精确的,甚至是错误的,因为上述三种情况都有参照完整性约束,但只有在最后一种情况下,孩子实际上被删除。 (除非它们是指具有依赖对象的参与识别关系的对象,这将是一个不寻常的术语。)


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 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) 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:

    • 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.

    • 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.

    • 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.

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天全站免登陆