Cascade Delete, same table, Entity Framework 4 Code First

白昼怎懂夜的黑 提交于 2020-01-02 03:42:05

问题


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

What can I do about this?

EDIT
I'm the only one with this problem?


回答1:


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.

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

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).



来源:https://stackoverflow.com/questions/4929611/cascade-delete-same-table-entity-framework-4-code-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!