级联删除问题?

编程入门 行业动态 更新时间:2024-10-23 06:28:48
本文介绍了级联删除问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用代码优先,数据注释,并试图避免流畅的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 Ibelieve that is availableto do this.

Dane

更多推荐

级联删除问题?

本文发布于:2023-10-16 05:46:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1496672.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:级联

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!