Guys I'm trying to run dotnet ef database update command but during the executing of it, it trows me an exception:
Failed executing DbCommand (12ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [ProductSubCategory] (
[ProductId] int NOT NULL,
[SubCategoryId] int NOT NULL,
CONSTRAINT [PK_ProductSubCategory] PRIMARY KEY ([SubCategoryId], [ProductId]),
CONSTRAINT [FK_ProductSubCategory_Product_ProductId] FOREIGN KEY ([ProductId]) REFERENCES [Product] ([ProductId]) ON DELETE CASCADE,
CONSTRAINT [FK_ProductSubCategory_SubCategory_SubCategoryId] FOREIGN KEY ([SubCategoryId]) REFERENCES [SubCategory] ([SubCategoryId]) ON DELETE CASCADE
);
Microsoft.Data.SqlClient.SqlException (0x80131904): Introducing FOREIGN KEY constraint 'FK_ProductSubCategory_SubCategory_SubCategoryId' on table 'ProductSubCategory' 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.
Introducing FOREIGN KEY constraint 'FK_ProductSubCategory_SubCategory_SubCategoryId' on table 'ProductSubCategory' 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'm using Sql Server as my database provider and Entity framework core 3.19 in my ASP.NET CORE MVC application. To be fair I'm not sure what is causing it, since i didn't have that troubles before, they appeared once I added foreign key properties such as public int CategoryId { get; set; } on all dependent entities (on one to many relationships) such as Product because i forgot to add them before, and now i will use them in order to update the Product Entities.I'm showing the entities and fluent api code
Product.cs
public class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public Category Category { get; set; }
public int CategoryId { get; set; } //It's one of the foreign keys properties added
public Brand Brand { get; set; }
public int BrandId { get; set; } //It's one of the foreign keys properties added
public string ImageName { get; set; }
public IEnumerable<ProductSubCategory> SubCategories { get; set; }
}
ProductSubCategory.cs
public class ProductSubCategory
{
public int ProductId { get; set; }
public Product Product { get; set; }
public int SubCategoryId { get; set; }
public SubCategory SubCategory { get; set; }
}
SubCategory.cs
public class SubCategory
{
public int SubCategoryId { get; set; }
public string Name { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
[JsonIgnore]
public IEnumerable<ProductSubCategory> ProductSubCategories { get; set; }
}
Category
public class Category
{
public int CategoryId { get; set; }
public string Name { get; set; }
public IEnumerable<Product> Products { get; set; }
public IEnumerable<SubCategory> SubCategories { get; set; }
}
DbContext.cs
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ProductSubCategory>()
.HasKey(psc => new {psc.SubCategoryId, psc.ProductId});
modelBuilder.Entity<ProductSubCategory>()
.HasOne(psc => psc.Product)
.WithMany(p => p.SubCategories)
.HasForeignKey(psc => psc.ProductId);
modelBuilder.Entity<ProductSubCategory>()
.HasOne(psc => psc.SubCategory)
.WithMany(sc => sc.ProductSubCategories)
.HasForeignKey(psc => psc.SubCategoryId);
}
I'm pretty sure i configure the relationship correctly.
Things i have tried:
- Delete the migration folder and created a new one.
- Update Ef Core nuget package to the latest version
- Dropping the database and then create a new one.
PD: Please i need help, i'm out of time, if you want the source code of the entities project with it's respective Dbcontext, i can give it to you, but as i said, it has 19 entities classes.
This is the source code: https://github.com/ToastedGuy2/Food-Town-issue
EDIT: The real question was How to remove On Cascade On Delete in every foreign key? It doesn't matter if it is One to Many or Many to many relationship.