引入 FOREIGN KEY 约束可能会导致循环或多个级联路径 [英] Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths

查看:24
本文介绍了引入 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; }

    }

会不会是商店的原因?

推荐答案

您模型中的所有关系都是必需的,因为所有外键属性(CountryIdRegionId, 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.

CountryRegion 有多个删除路径到 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 约束可能会导致循环或多个级联路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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