多个级联删除路径多次关系(EF 4.1) [英] multiple cascade delete path in many-many relationship (EF 4.1)

查看:115
本文介绍了多个级联删除路径多次关系(EF 4.1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表格:

 商店
产品
类别

关系:

 (Shop)1< ---> n(Categories)
(Shop)1< ---> n(产品)
(分类)n< ---> n(产品)

CASCADE DELETES:

  Shop --->类别...我使用流畅的API定义了这个API 
Shop --->产品...我使用流畅的API定义了这个API
分类< --->产品... EF 4.1自动为Category_Product连接表定义级联

问题:
以上会导致多重级联删除路径异常。



潜在固定:


  1. 删除ManyToManyConvention,但这意味着我必须手动为系统中的每个连接表执行删除,这是不切实际的。

  2. 我可以从Shop-> Category或Shop-> Products中删除级联删除。但是那么我可能会有很多孤立的记录。

你如何处理这个问题?
$ b

THANKS

解决方案

这不是实体框架的问题,而是SQL Server的问题。我不认为异常实际上意味着圆形级联删除。更可能意味着多个级联删除路径,因为连接表记录可以从类别和产品方面删除,因为从商店中级联。 SQL Server不允许这样做,因为它需要一些更复杂(和慢)的算法来正确计算哪些记录,何时必须在级联时删除。



只需要打破这一点,这将意味着您必须在删除商店之前手动删除所有相关记录(类别或产品)。这将需要存储过程(或直接SQL DELETE命令),否则您将必须首先加载它们,并逐个删除。



编辑:



正如你所指出的那样,这也可以通过添加BEFORE DELETE触发来解决,如果存在替代一个级联路径,那将删除相关记录。


THE TABLES:

Shop
Product
Category

THE RELATIONSHIPS:

      (Shop) 1 <---> n (Categories)
      (Shop) 1 <---> n (Products)
(Categories) n <---> n (Products)

THE CASCADE DELETES:

      Shop  ---> Categories ... I defined this using fluent API
      Shop  ---> Products   ... I defined this using fluent API
Categories <---> Products   ... EF 4.1 automatically defines cascade for "Category_Product" join table

THE PROBLEM: Above results in a "multiple" cascade deletion path exception.

POTENTIAL FIXES:

  1. Remove the ManyToManyConvention, but that means I must manually perform deletes for every join table in the system, which is impractical.
  2. I can remove the cascade delete from Shop->Category or Shop->Products. But then I'll probably have lots of orphaned records.

How are you folks dealing with this problem?

THANKS

解决方案

This is not a problem of entity framework but the problem of SQL server. I don't think that exception actually means circular cascade delete. It more probably means multiple cascade delete paths because join table records can be deleted from both categories and products side because of cascading from shop. SQL server doesn't allow this because it requires some more complex (and slow) algorithms to correctly compute which records and when have to be deleted when cascading.

Simply you must break this and it will really mean that you will have to manually delete all related records (either categories or products) before you delete shop. This will require stored procedure (or direct SQL DELETE command) otherwise you will have to load all of them first and delete them one by one.

Edit:

As you pointed in the comment this can be also solved by adding BEFORE DELETE trigger which will delete related records if exists as replacement of one cascade path.

这篇关于多个级联删除路径多次关系(EF 4.1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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