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.

EF Core 中,级联更新是指在更新一个实体时,自动更新与之关联的其他实体。EF Core 提供了几种级联更新的选项,可以根据需要选择适合的方式。 1. 配置级联更新:可以通过 Fluent API 或者数据注解来配置级联更新行为。例如,可以使用 `OnDelete` 方法来配置关系的删除行为,从而实现级联更新。例如,可以使用 `OnDelete(DeleteBehavior.Cascade)` 来配置级联删除。 2. 手动处理级联更新:在某些情况下,可能需要手动处理级联更新。可以在更新操作之前获取关联的实体,并手动更新它们的属性。这需要在代码中显式地处理级联更新逻辑。 3. 使用 Include 方法加载关联实体:在查询数据时,可以使用 Include 方法来加载与之关联的实体,以便在更新时一并更新这些实体。例如,使用 `Include` 方法加载关联实体,然后对它们进行修改后再保存更改。 4. 使用导航属性来进行级联更新:在 EF Core 中,使用导航属性可以方便地进行级联更新。通过设置导航属性的值,可以自动更新关联的实体。例如,通过设置导航属性的引用,可以自动更新外键关系。 需要注意的是,在使用级联更新时,应谨慎处理循环引用的情况,以避免无限递归和性能问题。可以通过配置导航属性的 `OnDelete` 行为或手动处理级联更新来解决循环引用的问题。 这些是在 EF Core 中实现级联更新的一些常见方法和技巧。根据具体的业务需求和数据模型,可以选择适合的方式来处理级联更新操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值