简体   繁体   English

如何删除实体框架中的多个级联路径

[英]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:我的关系是由实体框架创建的:

  1. One Advert has many QUOTES, PRODUCTS:一个广告有很多报价,产品:
public List<Quote> Quotes { get; set; } = new List<Quote>();
public List<Product> Products { get; set; } = new List<Product>();
  1. One Quote has many QPRODUCTS and one ADVERT:一个报价有多个 QPRODUCTS 和一个 ADVERT:
public Advert Advert { get; set; }
public int AdvertId { get; set; }

public List<QProduct> QProducts { get; set; } = new List<QProduct>();
  1. Product has one ADVERT, many QPRODUCTS:产品有一个ADVERT,多个QPRODUCTS:
public int AdvertId { get; set; }
public Advert Advert { get; set; }

public List<QProduct> QProducts { get; set; } = new List<QProduct>();
  1. QProduct has one QUOTE, one PRODUCT: QProduct 有一个 QUOTE,一个 PRODUCT:
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.

相关问题 实体框架多个级联路径错误 - entity framework multiple cascade paths error 实体框架级联删除-循环或多个级联路径 - Entity Framework cascade delete - cycles or multiple cascade paths 首先使用实体​​框架代码:循环或多个级联路径 - Entity Framework Code first: cycles or multiple cascade paths Entity Framework Core:可能导致循环或多个级联路径 - Entity Framework Core: may cause cycles or multiple cascade paths 多个级联路径错误实体框架代码优先 - Multiple cascade paths error entity framework Code first .Net实体框架循环级联路径 - .Net Entity Framework Cyclic Cascade Paths 如何首先使用实体​​代码解决多个级联路径 - How to solve multiple cascade paths with entity code first MVC 4实体框架同一表的两个外键导致循环或多个级联路径 - MVC 4 Entity Framework Two Foreign Keys to the Same Table Cause cycles or multiple cascade paths SQL多个级联路径/无法首先确定类型实体框架代码之间的关联的主体端 - Sql multiple cascade paths/ Unable to determine the principal end of an association between the types entity framework code first 实体框架C#在表Y中引入外键X可能会导致循环或多个级联路径 - Entity Framework C# Introducing A Foreign Key X In Table Y May Cause Cycles OR Multiple Cascade Paths
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM