简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM