引入 FOREIGN KEY 约束可能会导致循环或多个级联路径

编程入门 行业动态 更新时间:2024-10-25 20:27:27
本文介绍了引入 FOREIGN KEY 约束可能会导致循环或多个级联路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我收到此错误

引入 FOREIGN KEY 约束表 'Regions' 上的 'FK_dbo.Regions_dbo.Countries_CountryId' 可能会导致循环或多个级联路径.指定 ON DELETE NO ACTION 或 ONUPDATE NO ACTION,或修改其他 FOREIGN KEY 约束.不能创建约束.查看以前的错误.

Introducing FOREIGN KEY constraint 'FK_dbo.Regions_dbo.Countries_CountryId' on table 'Regions' 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. See previous errors.

我想知道这是否意味着我的数据库设计不好?我读到你关闭了级联或类似的东西,但我不确定这是否能解决问题.

I am wondering does this mean my database design is bad? I read you turn off the cascades or something like that but I am just not sure if that is sweeping the problem out of the rug.

我只是让 EF 通过我的域类生成我的表(此时我没有使用任何数据注释或流畅的映射).

I am just letting EF generate my tables through my domain class(I am not using any data annotations or fluent mapping at this point).

public class Country { public Country() { this.Stores = new List<Store>(); this.Regions = new List<Region>(); Id = GuidCombGenerator.GenerateComb(); } public Guid Id { get; private set; } private string name; public string Name { get { return name; } set { name = value.Trim(); } } private string code; public string Code { get { return code; } set { code = value.Trim(); } } public virtual ICollection<Store> Stores { get; set; } public virtual ICollection<Region> Regions { get; set; } } public class City { public City() { this.Stores = new List<Store>(); Id = GuidCombGenerator.GenerateComb(); } public Guid Id { get; private set; } private string name; public string Name { get { return name; } set { name = value.Trim(); } } public Guid RegionId { get; set; } public virtual Region Region { get; set; } public virtual ICollection<Store> Stores { get; set; } } public class Region { public Region() { this.Cities = new List<City>(); this.Stores = new List<Store>(); Id = GuidCombGenerator.GenerateComb(); } public Guid Id { get; private set; } private string state; public string State { get { return state; } set { state = value.Trim(); } } public Guid CountryId { get; set; } public virtual ICollection<City> Cities { get; set; } public virtual Country Country { get; set; } public virtual ICollection<Store> Stores { get; set; } } public class Store { public Store() { Id = GuidCombGenerator.GenerateComb(); Users = new List<User>(); } public Guid Id { get; private set; } public Guid CountryId { get; set; } public Guid CityId { get; set; } public Guid RegionId { get; set; } public virtual City City { get; set; } public virtual Country Country { get; set; } public virtual Region Region { get; set; } public virtual ICollection<User> Users { get; set; } }

会不会是商店的原因?

推荐答案

您模型中的所有关系都是必需的,因为所有外键属性(CountryId、RegionId, CityId) 不可为空.对于必需的一对多关系,EF 将按照约定启用级联删除.

All relationships in your model are required because all foreign key properties (CountryId, RegionId, CityId) are not nullable. For required one-to-many relationships EF will enable cascading delete by convention.

Country 和 Region 有多个删除路径到 Store 表,例如,如果您删除 Country可以通过三种不同的级联路径(SQL Server 不允许)删除相关的 Store:

Country and Region have multiple delete paths to the Store table, for example if you delete a Country the related Stores can be deleted via three different cascading paths (which is not allowed with SQL Server):

  • 国家 -> 商店
  • Country -> Region -> Store
  • Country -> Region -> City -> Store
  • Country -> Store
  • Country -> Region -> Store
  • Country -> Region -> City -> Store

您必须通过使用 Fluent API 禁用级联删除或将某些关系定义为可选(使用可为空的外键 Guid?)来避免此类模糊的删除路径.

You must avoid such ambiguous delete paths by either disabling cascading delete using Fluent API or by defining some of the relationships as optional (with a nullable foreign key Guid?).

或者从除 City 之外的所有实体中移除 Stores 集合(以及反向引用和 FK 属性).对我来说,这些集合看起来是多余的,因为您可以通过浏览 Regions.Cities.Stores 集合来找到 Country 中的所有商店.

Or remove the Stores collections (and the inverse references and FK properties) from all entities except City. To me those collections look redundant because you can find all stores in a Country by navigating through the Regions.Cities.Stores collections.

更多推荐

引入 FOREIGN KEY 约束可能会导致循环或多个级联路径

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

发布评论

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

>www.elefans.com

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