级联删除问题? [英] Cascade Delete question?

查看:81
本文介绍了级联删除问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用代码优先,数据注释,并试图避免流畅的api。


< rant>我真的很沮丧试图完成简单事情......< / rant>


我有一个对象/表,StockOrder对Location对象/表有一个引用/ fk


我有另一个表,组织,也有一个参考/ fk到位置。


我在创建数据库时收到错误


在表'StockOrders'上引入FOREIGN KEY约束'StockOrder_OrderedFromLocation'可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。

无法创建约束。


我需要什么在这做?我*假设*删除StockOrder将导致"位置"被删除,如果"组织",这会导致混淆被删除?我不想要任何级联,所以如何关闭它?如果我必须使用Fluent API,有人可以将
指向我的示例代码吗?否则,我如何使用数据注释来装饰它。


非常感谢帮助...我花了2天时间仍然没有简单,真实的数据库设置。

解决方案

我也遇到了这个问题,不得不关闭级联删除,默认情况下代码首先打开。 在OnModelCreating的模型类中,你需要添加一些代码来关闭它:

 modelBuilder.Entity< 实体>()
.HasMany(u  =>  u.Collection)
.WithRequired(a  =>  a.Entity)
.WillCascadeOnDelete( false );

 通过流畅的API添加此行后,它全部工作了对我来说。 不幸的是,使用流利的API是我认为可以执行此操作的唯一方式。


 


Dane


I'm using code-first, data annotations, and trying to avoid the fluent api.

<rant>I'm getting really frustrated trying to accomplish simple things...</rant>

I have a object/table, StockOrder that has a reference/fk to a Location object/table

I have another table, Organization, that also has a reference/fk to Location.

I am getting the error, when creating the database

Introducing FOREIGN KEY constraint 'StockOrder_OrderedFromLocation' on table 'StockOrders' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint.

What do I need to do here? I am *assuming* that deletion of StockOrder will cause the "Location" to be deleted, and this causes conflect if "Organization" gets deleted? I don't want any cascade, so how do I turn this off? If I have to use the Fluent API, can someone point me to the sample code? Otherwise, how do I decorate this with data annotation.

Help would be much appreciated... I've spent 2 days and I still don't have a simple, realistic database setup yet.

解决方案

I ran into this issue too and had to turn off cascading deletes, which code first has on by default.  In your model class in the OnModelCreating you willl need to add some code to turn this off:

modelBuilder.Entity<Entity>()
.HasMany(u => u.Collection)
.WithRequired(a => a.Entity)
.WillCascadeOnDelete(false);

 After adding this line through the fluent API it all worked for me.  Unfortunately using the fluent API is the only way I believe that is available to do this.

 

Dane


这篇关于级联删除问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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