级联删除,同一表,实体框架4代码第一 [英] Cascade Delete, same table, Entity Framework 4 Code First

查看:116
本文介绍了级联删除,同一表,实体框架4代码第一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Im目前正在使用.sdf数据库(Server Compact V4.0)和sql express。我试图在同一个表(类别 - 子类别)上设置一个级联删除,但我得到我不能添加到同一个表的关系。

Hi Im currently working with .sdf database (Server Compact Version 4.0) and sql express. I'm trying to setup a cascade delete on a same table (category - sub category) but I get that I cant add relation to the same table.


外键约束和
更新或删除级联规则,
自引用一列相同的
表不允许

A foreign key constraint had and update or a delete cascade rule, and self-references a column in the same table, is not allowed

我可以做些什么?

编辑

我是唯一有这个问题的人?

EDIT
I'm the only one with this problem?

推荐答案

正如您所提出的SQLException一样,这是SQL Server的一般限制,与EF或Code First无关。基本上,SQL Server不允许在内部关系上创建级联操作 - 当级联路径从表A中的列col1到表A中的列col2时。A-> A。

As your SQLException suggested, this is a limitation of SQL Server in general and has nothing to do with EF or Code First. Basically, SQL Server does not allow creating cascade actions on Inner relationships – when the cascade path goes from column col1 in table A to column col2 also in table A. A->A.

事实上,Code First正在尝试使用声明性引用完整性(DRI)来执行级联删除和SQL Server throws。

In fact, Code First was trying to use Declarative Referential Integrity (DRI) to enforce cascade deletes and SQL Server throws.

为此关系执行级联删除的唯一方法是使用触发器。您可以在类别表上写入删除触发器,删除相关行或将所有对应的外键设置为NULL(根据您的要求)。

The only way to enforce cascade deletes for this relationship is to use Triggers. You can write a Delete Trigger on the category table that either deletes the dependent rows or sets all corresponding foreign keys to NULL (based on your requirements).

这篇关于级联删除,同一表,实体框架4代码第一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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