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

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

问题描述

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

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

<一个href=\"http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.deleteobject.aspx\">ObjectContext.DeleteObject方法

首先是在 EntityCollection 和第二的的ObjectContext

在应该使用哪一个?

是一个prefered比其他?

Is one prefered over the other?

删除()返回布尔 DeleteObject的()返回无效

推荐答案

这不是一般正确的,你可以用两种方法的从数据库中的删除项目。要precise它就像这样:

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(实体)标记的实体为删除 在上下文中。 (这是 EntityState 删除之后)。如果你打电话的SaveChanges 事后EF发送SQL 删除语句到数据库。如果受到侵犯的实体数据库中没有引用约束将被删除,否则会抛出异常。

  • 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)标记的父之间的关系, childEntity 删除 。如果 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 NULL childEntity 将被写入到数据库中(即两者之间的关系被删除)。这种情况与SQL 更新语句。否删除语句时发生。

  • 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会抛出异常 - 臭名昭著的<一个href=\"http://stackoverflow.com/questions/5538974/the-relationship-could-not-be-changed-because-one-or-more-of-the-foreign-key-pro\">The关系不能被改变,因为一个或多个外键的属性是不可为空的异常或相似的。

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 删除为好。如果你调用的SaveChanges A SQL 删除语句将被发送到数据库中。如果受到侵犯的实体数据库中没有其他参照约束将会被删除,否则会抛出异常。

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页面你已经挂在备注部分因为它说:如果关系具有参照完整性约束,要求从属对象标记删除方法既关系,并为删除依赖对象的。这似乎未precise甚至是错误的我,因为上述所有三种情况下有一个的引用完整性约束的,但只在最后一种情况的孩子,其实删除。 (除非他们与平均的依赖对象的,在一个确定的关系参加对象这将是一个不寻常的术语虽然。)

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

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

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