[英]How to remove multiple cascade paths in entity framework
I am trying to define relationships between tables, but getting the following error:我正在尝试定义表之间的关系,但出现以下错误:
Introducing FOREIGN KEY constraint 'FK_QProducts_Quotes_QuoteId' on table 'QProducts' may cause cycles or multiple cascade paths.
在表“QProducts”上引入 FOREIGN KEY 约束“FK_QProducts_Quotes_QuoteId”可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.Could not create constraint or index.
指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。无法创建约束或索引。 See previous errors.
查看以前的错误。
This is how my tables look like:这是我的表的样子:
What I am trying to achieve:我想要达到的目标:
A. Client creates an inquiry and adds products and quantities that he wish to receive a quote for A. 客户创建询价并添加他希望收到报价的产品和数量
B. Suppliers quote and write prices for each product B. 供应商为每个产品报价和写价
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. C.
Products
表包含有关数量的信息, QProducts
表在报价期间使用价格等信息扩展它,因此我可以在报价下显示产品的数量和价格。
But the error is not allowing to update-database.但错误是不允许更新数据库。 How could I remove this error?
我怎样才能消除这个错误?
My relations are created by entity framework:我的关系是由实体框架创建的:
public List<Quote> Quotes { get; set; } = new List<Quote>();
public List<Product> Products { get; set; } = new List<Product>();
public Advert Advert { get; set; }
public int AdvertId { get; set; }
public List<QProduct> QProducts { get; set; } = new List<QProduct>();
public int AdvertId { get; set; }
public Advert Advert { get; set; }
public List<QProduct> QProducts { get; set; } = new List<QProduct>();
public Quote Quote { get; set; }
public int QuoteId { get; set; }
public Product Product { get; set; }
public int ProductId { get; set; }
Put simply, you cannot have cascade delete down to QProduct via multiple paths, which you do at the moment.简而言之,您不能通过多条路径将级联删除向下删除到 QProduct,而您目前正在这样做。 You need to turn Cascade Delete off of one (or both, if you prefer).
您需要关闭其中一个(或两个,如果您愿意)的级联删除。
For example, when configuring Product, try:例如,在配置 Product 时,尝试:
HasMany(p => p.QProducts)
.WithOne(q => q.Product)
.HasForeignKey(q => q.ProductId)
.OnDelete(DeleteBehavior.Restrict);
You'll probably need to make the FK nullable also:您可能还需要使 FK 可为空:
public int? ProductId { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.