如何在实体框架中删除多个级联路径 [英] How to remove multiple cascade paths in entity framework

查看:75
本文介绍了如何在实体框架中删除多个级联路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试定义表之间的关系,但是出现以下错误:

I am trying to define relationships between tables, but getting the following error:

在表'QProducts'上引入FOREIGN KEY约束'FK_QProducts_Quotes_QuoteId'可能会导致循环或多个级联路径.指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束.无法创建约束或索引.请参阅先前的错误.

Introducing FOREIGN KEY constraint 'FK_QProducts_Quotes_QuoteId' on table 'QProducts' 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 or index. See previous errors.

这是我的桌子的样子:

我要实现的目标:

A.客户创建查询并添加希望收到报价的产品和数量

A. Client creates an inquiry and adds products and quantities that he wish to receive a quote for

B.供应商为每种产品报价并写出价格

B. Suppliers quote and write prices for each product

C. Products 表包含有关数量的信息,而 QProducts 表在报价过程中扩展了价格等信息,因此我可以在报价下显示具有数量和价格的产品.

C. Products table contains information about quantity and QProducts table extends it during quote with information such as prices, so I could display products under quote with both quantities and prices.

但是该错误不允许更新数据库.我该如何删除该错误?

我的关系是通过实体框架创建的:

My relations are created by entity framework:

  1. 一个广告有很多报价,产品:

public List<Quote> Quotes { get; set; } = new List<Quote>();
public List<Product> Products { get; set; } = new List<Product>();

  1. 一个报价包含多个QPRODUCTS和一个ADVERT:

public Advert Advert { get; set; }
public int AdvertId { get; set; }

public List<QProduct> QProducts { get; set; } = new List<QProduct>();

  1. 产品有一个ADVERT,许多QPRODUCTS:

public int AdvertId { get; set; }
public Advert Advert { get; set; }

public List<QProduct> QProducts { get; set; } = new List<QProduct>();

  1. QProduct有一个QUOTE,一个PRODUCT:

public Quote Quote { get; set; }
public int QuoteId { get; set; }

public Product Product { get; set; }
public int ProductId { get; set; }

推荐答案

简单地讲,您不能通过多个路径将其级联删除到QProduct上,这是您目前要做的.您需要关闭级联删除"中的一个(或两个,如果您愿意的话).

Put simply, you cannot have cascade delete down to QProduct via multiple paths, which you do at the moment. You need to turn Cascade Delete off of one (or both, if you prefer).

例如,在配置产品时,请尝试:

For example, when configuring Product, try:

HasMany(p => p.QProducts)
   .WithOne(q => q.Product)
   .HasForeignKey(q => q.ProductId)
   .OnDelete(DeleteBehavior.Restrict);

您可能还需要将FK设置为​​可空值

You'll probably need to make the FK nullable also:

public int? ProductId { get; set; }

这篇关于如何在实体框架中删除多个级联路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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