gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:51:44 27 4
gpt4 key购买 nike

我收到此错误

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.



我想知道这是否意味着我的数据库设计不好?我看过你关闭了串级联或类似的功能,但我不确定这是否能将问题排除在地毯之外。

我只是让EF通过我的域类生成我的表(此时我不使用任何数据注释或流畅的映射)。
       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; }

}

可能是因为商店吗?

最佳答案

模型中的所有关系都是必需的,因为所有外键属性(CountryIdRegionIdCityId)都不能为空。对于所需的一对多关系,EF将启用按惯例级联删除。
CountryRegion具有到Store表的多个删除路径,例如,如果删除Country,则可以通过三个不同的级联路径(SQL Server不允许)删除相关的Store:

  • Country-> Store
  • Country-> Region-> Store
  • Country-> Region-> City-> Store

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

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

    关于entity-framework - 引入FOREIGN KEY约束可能会导致循环或多个级联路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19373310/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com