EF 遭遇级联删除失败

11 篇文章 0 订阅
3 篇文章 0 订阅

最近利用 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值