0

I am getting this error in code first MVC CORE while inserting this and I am doing Code first for very first Time command:

Update-Database -Context ProjectDbContext

"Introducing FOREIGN KEY constraint 'FK_Product_SubCategory_SubCategoryId' on table 'Product' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints".

My Classes are are given below and also explain me why we use association in mvc model classes like this :

public virtual SubCategory SubCategory { get; set; }

Or
public virtual ICollection<Category> Category { get; set; }

public virtual Icollection<SubCategory> SubCategory { get; set; }



public class Product
{

    public int ProductId { get; set; }

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

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

    [Required]
    public int Quatity { get; set; }

    [Required]
    public int Price { get; set; }

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

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

    public byte Status { get; set; }

    public DateTime TodayDate { get; set; }

    [Required]
    public int CategoryId { get; set; }

    [Required]
    public int SubCategoryId { get; set; }

    public virtual Category Category { get; set; }
    public virtual SubCategory SubCategory { get; set; }
}

public class SubCategory
{
    public int SubCategoryId { get; set; }

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

    [Required]
    public int CategoryId { get; set; }

    public ICollection< Category> Category { set; get; }

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


public class Category
{
    public int CategoryId { set; get; }

    [Required]
    public string Name { get; set; }
}
itminus
  • 23,772
  • 2
  • 53
  • 88
Syed M Usama
  • 31
  • 1
  • 3
  • have you read this [thread](https://stackoverflow.com/questions/17127351/introducing-foreign-key-constraint-may-cause-cycles-or-multiple-cascade-paths) ? – itminus Nov 29 '18 at 08:43
  • What is the relationship between `Product` and `SubCategory`? Will one product must have `SubCategory`? – Edward Dec 03 '18 at 07:21
  • Share us your code in `ProjectDbContext`. I fail to reproduce your issue with ` public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions options) : base(options) { } public virtual DbSet SubCategory { get; set; } public virtual DbSet Category { get; set; } public virtual DbSet Product { get; set; } } ` – Edward Dec 03 '18 at 07:29
  • these are my classes public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions options) : base(options) { } public DbSet Product { get; set; } public DbSet Category { get; set; } public DbSet SubCategory { get; set; } public DbSet Country { get; set; } } – Syed M Usama Dec 03 '18 at 07:58
  • Problem Solved by removing required on CategoryId and SubcategoryId. – Syed M Usama Dec 04 '18 at 08:05

1 Answers1

0

add Following in identity model file under ApplicationDbContext class

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {            
        base.OnModelCreating(modelBuilder);
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    }

hope this will help but your cascade delete will be off.

  • DbModelBuilder is not existing. Can you tell me its assembly/dll name? If I am using ModelBuilder then Conventions.Remove() is not exiting. – Syed M Usama Nov 29 '18 at 09:38