简体   繁体   English

实体框架C#在表Y中引入外键X可能会导致循环或多个级联路径

[英]Entity Framework C# Introducing A Foreign Key X In Table Y May Cause Cycles OR Multiple Cascade Paths

I am developing a C# MVC application. 我正在开发C#MVC应用程序。 I am using Code First approach to model my database. 我正在使用代码优先方法对数据库进行建模。

My project had the following requirements: 我的项目具有以下要求:

  1. Company Can Have Many Products 公司可以有很多产品
  2. Product Can have many Advertisement Types 产品可以有多种广告类型

Here are model classes (code first solution) to the above mentioned problem. 这是上述问题的模型类(代码优先解决方案)。

    public class Company
    {

        public Company()
        {
            this.Employees = new HashSet<ApplicationUser>();
        }

        public int ID { get; set; }

        [Required]
        public string Name { get; set; }

        public string Logo { get; set; }
        [Display(Name="Company Description")]
        public string CompanyDescription { get; set; }

        public DateTime Created { get; set; }

        public DateTime Updated { get; set; }

        public virtual ICollection<ApplicationUser> Employees { get; set; }

        public virtual ICollection<Client> Clients { get; set; }

        public ICollection<Product> Products { get; set; }
    }

    public class Product
    {
       public int ProductID { get; set; }

       public DateTime Created { get; set; }

       public DateTime Updated { get; set; }

       public string ProductName { get; set; }

       public int CompanyID { get; set; }

       public virtual Company Company { get; set; }

       public virtual ICollection<AdvertisementType> AdvertisementTypes { get; set; }
    }

    public class AdvertisementType
    {
       public int AdvertisementTypeID { get; set; }

       public int ProductID { get; set; }

       [Display(Name = "Advertisement Name")]
       public string AdvertisementTypeName { get; set; }

       public DateTime Created { get; set; }

       public DateTime Updated { get; set; }

       public virtual Product Product { get; set; }
    }

When I try to update the database, after creating the migrations i get the following error: 当我尝试更新数据库时,在创建迁移后出现以下错误:

Introducing FOREIGN KEY constraint 'FK_dbo.AdvertisementTypes_dbo.Products_ProductID' on table 'AdvertisementTypes' 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 or index. 无法创建约束或索引。 See previous errors. 请参阅先前的错误。

I have been trying solve this problem but unable to find any solution. 我一直在尝试解决此问题,但找不到任何解决方案。 I don't find any problem with the model classes, nor i think there are any issues with the relationship between the models. 我发现模型类没有任何问题,我也不认为模型之间的关系有任何问题。

Any suggestions or help will be useful. 任何建议或帮助将是有用的。

EDIT 编辑

Here is screenshot of Tables and their relations 这是表格及其关系的屏幕截图

在此处输入图片说明

in your dbContext you need to turn cascade delete to false if you want to avoid that. 在dbContext中,如果要避免这种情况,需要将层叠删除设置为false。

protected override void OnModelCreating(ModelBuilder modelBuilder)

{

    modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

}

You can initialize your make a List() in Product like you did with Company class... 您可以像使用Company类一样在Product中初始化make list()。

public class Product
{
   public int ProductID { get; set; }

public Product()
    {
        this.AdvertisementTypes = new List<AdvertisementType>();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ef核心2-在表&#39;Y&#39;上引入FOREIGN KEY约束&#39;X&#39;可能会导致循环或多个级联路径 - ef core 2 - Introducing FOREIGN KEY constraint 'X' on table 'Y' may cause cycles or multiple cascade paths 实体框架:在表 '' 上引入 FOREIGN KEY 约束 '' 可能会导致循环或多个级联路径 - Entity Framework: Introducing FOREIGN KEY constraint '' on table '' may cause cycles or multiple cascade paths SQL 错误:引入 FOREIGN KEY 约束可能会导致循环或多个级联路径。 实体框架核心 - SQL Error: Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths. Entity Framework Core 引入 FOREIGN KEY 约束...可能导致循环或多个级联路径 - 实体框架 - Introducing FOREIGN KEY constraint ... may cause cycles or multiple cascade paths - Entity Framework 在表上引入外键约束可能会导致循环或多个级联路径 - Introducing Foreign key Constraint on table may cause cycles or multiple cascade paths 在表“模型”上引入FOREIGN KEY约束“列”可能会导致循环或多个级联路径 - Introducing FOREIGN KEY constraint 'Column' on table 'Model' may cause cycles or multiple cascade paths 表&#39;UsageSummaries&#39;上的多态与引入FOREIGN KEY约束可能会导致循环或多个级联路径 - Polymorphism vs Introducing FOREIGN KEY constraint '' on table 'UsageSummaries' may cause cycles or multiple cascade paths 在表table上引入FOREIGN KEY约束键可能会导致循环或多个级联路径。 指定ON DELETE…错误 - Introducing FOREIGN KEY constraint key on table table may cause cycles or multiple cascade paths. Specify ON DELETE … Error 实体框架,外键约束可能会导致循环或多个级联路径 - Entity Framework, Foreign key constraint may cause cycles or multiple cascade paths 实体框架代码优先外键可能会导致循环或多个级联路径 - Entity Framework Code first Foreign Key may cause cycles or multiple cascade paths
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM