您的位置:首页 > 其它

EF 遭遇级联删除失败

2015-08-18 10:47 323 查看
最近利用 ef写代码,(数据库更新的ef),然后通过自建关系来连接各个数据实体。在关系里设置END1 ON DELETE 为cascade ,非常可惜居然无法进行级联删除,在网上查阅了大量资料发现问题竟然是存储的ef数据里没有级联删除的实体的数据,这也难怪,ef里没有使用到的数据他是不会加载到内存的。所以引用老外的一篇文章以作参考

实在没辙 只有让ef加载一下内存才可以。然后就有了以下的方法

1.首先在ef实体框架里 设置好END1 ON DELETE 为cascade。

2.写删除程序的时候,把要级联删除的那部分加载一下如下

var db = new InvestP2PEntities();

var rm = db.Company.First(p => p.Id == 1);
//设置了end1 on delete cascade 然后把要级联删除的那部分加载一下
var temp=rm.govinorg_gov;//这个是导航属性
temp=rm.govinorg_org;//这个是导航属性
//加载完毕
db.Company.Remove(rm);

db.SaveChanges();


Imagine that in your database you have a cascade delete on an FK relationship.

Something like this:

CascadeDeleteInDatabase

Here the Delete Rule says that when a Category is deleted all the related Products should be deleted too.

If you generate an EF model from this database you get a model that on the surface looks no different from normal:

ProductCategory

But if you dig into the CSDL section of the XML you will see this:

Notice the element, this tells the EF that when a Category is deleted the related Products will be too.

I deliberately said will and rather than should, because the EF does not take responsibility for cascading the delete in the database.

The EF is responsible for the correctness of the ObjectContext after SaveChanges(). So the EF attempts to synchronize the ObjectContext, with the expected database state after the expected cascade in the database.

A tell tale sign of this is that if you open up something like SqlProfiler, you will notice the EF issuing DELETE requests for dependent entities that it knows about (i.e. that are loaded in the ObjectContext) when a principal is deleted.

Essentially what is happening here is that the Entity Framework expects that deleting the principal in the database, will delete all it’s dependents in the database. So it issues, what should be, a redundant DELETE to request itself so the dependents already loaded are deleted from the ObjectContext.

The key thing to note is that the EF does not retrieve all the dependent entities and issue deletes for them: It only deletes dependents that are already in memory.

So here are the golden rules:

If you add an Cascade delete rule to the model, you MUST have a corresponding DELETE rule in the database.

If you absolutely insist on breaking rule (1) for some reason, Cascade will only work if you have all the dependents loaded in memory.

(2) is not recommended!!!

While we do our best to keep the ObjectContext and database in sync, our attempts can fail if you have multiple levels of cascade delete.

For example, if you have this:

Category –> Product –> Order

And deleting a Category deletes its Products which in turn deletes its Orders.

The EF can, in rare circumstances, fail to sync up with the database when you delete a Category.

For example if you have an Order loaded that is related to a Category via an unloaded Product, and you delete the Category,the EF won’t know to delete the Order.

This means the Order will remain in the ObjectContext in the unchanged state, despite it having been deleted in the database.

Forewarned is forearmed.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: