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

查看:151
本文介绍了引入FOREIGN KEY约束可能会导致循环或多个级联路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到这个错误


介绍FOREIGN KEY约束
'FK_dbo.Regions_dbo.Countries_CountryId'可能导致
个循环或多个级联路径。指定ON DELETE NO ACTION或ON
UPDATE 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 ),模型中的所有关系都需要 code>, 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.

国家区域有多个删除路径到 Store 表,例如,如果您删除一个国家相关的存储可以通过三个不同的级联路径删除(SQL Server不允许):

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 -> 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?).

或从所有权限中删除 Stores 集合(以及反向引用和FK属性)除了城市。对我来说,这些集合看起来是多余的,因为您可以通过浏览 Regions.Cities.Stores 国家/地区中找到所有商店。收藏。

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天全站免登陆