I have 3 main entities : Category , SubCategory , Products:
public class Product
{
public Product()
{
Photos = new List<Photo>();
Comments = new List<Comment>();
Colors = new List<ProductColor>();
Attributes = new List<ProductAttributes>();
}
public int Id { get; set; }
public string ProductName { get; set; }
public string BrandName { get; set; }
public string SellerName { get; set; }
public decimal Price { get; set; }
public bool OnSale { get; set; }
public int SalePercantage { get; set; }
public DateTime DateAdded { get; set; }
public int UnitsInStock { get; set; }
public string MainImageUrl
{
get
{
return Photos.Count > 0 ? Photos[0].PhotoPath : "https://dummyimage.com/600x600/e0d0e0/ffffff.png";
}
}
public decimal ShownPrice
{
get
{
if (OnSale)
{
return SalePrice;
}
else
{
return Price;
}
}
}
public decimal SalePrice
{
get
{
return (decimal)((decimal)Price - ((decimal)Price * ((decimal)SalePercantage / (decimal)100)));
}
}
public bool IsNewBrand
{
get
{
return DateTime.Now.AddDays(-3) <= DateAdded;
}
}
public int SubCategoryId { get; set; }
public SubCategory SubCategory { get; set; }
public List<Photo> Photos { get; set; }
public List<Comment> Comments { get; set; }
public List<ProductColor> Colors { get; set; }
public List<ProductAttributes> Attributes { get; set; }
public List<ProductCategory> ProductCategories { get; set; }
public List<ProductProductSize> ProductSizes { get; set; }
}
public class Category
{
public int Id { get; set; }
public string CategoryName { get; set; }
public List<ProductCategory> ProductCategories { get; set; }
public List<SubCategory> SubCategories { get; set; }
}
public class SubCategory
{
public int Id { get; set; }
public string Name { get; set; }
public List<Product> Products { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
}
In my DbContext, I define my relationships:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ProductCategory>().HasKey(p => new { p.CategoryId, p.ProductId });
modelBuilder.Entity<ProductCategory>().HasOne(p => p.Product).WithMany(d => d.ProductCategories).HasForeignKey(p => p.ProductId);
modelBuilder.Entity<ProductCategory>().HasOne(p => p.Category).WithMany(d => d.ProductCategories).HasForeignKey(p => p.CategoryId);
modelBuilder.Entity<SubCategory>().HasOne(p => p.Category).WithMany(d => d.SubCategories).HasForeignKey(p => p.CategoryId);
modelBuilder.Entity<Product>().HasOne(d => d.SubCategory).WithMany(p => p.Products).HasForeignKey(d => d.SubCategoryId);
}
When I'm trying to migrate my database from PowerShell, I'm getting this error:
Introducing FOREIGN KEY constraint 'FK_ProductCategory_Products_ProductId' on table 'ProductCategory' 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 couldn't recognize the issue here, what am I doing wrong?
Shouldn't I have the HasOne.WithMany code with my ProductCategory entity or something like that?
I tried almost everything and could not find the solution yet.