我有3个主要实体:类别,子类别,产品:
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; }
}
在我的DbContext
中,我定义了自己的关系:
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);
}
当我尝试从PowerShell迁移数据库时,出现此错误:
在表'ProductCategory'上引入FOREIGN KEY约束'FK_ProductCategory_Products_ProductId'可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。
无法创建约束或索引。查看以前的错误。
我在这里无法识别问题,我在做什么错了?
我的HasOne.WithMany
实体中不应该有ProductCategory
代码吗?
我几乎尝试了所有操作,但找不到解决方案。
答案 0 :(得分:0)
在.OnDelete(DeleteBehavior.Restrict)
Fluent API 配置中指定ProductCategory
,如下所示:
modelBuilder.Entity<ProductCategory>()
.HasOne(p => p.Product)
.WithMany(d => d.ProductCategories)
.HasForeignKey(p => p.ProductId)
.OnDelete(DeleteBehavior.Restrict); // <-- Here it is
modelBuilder.Entity<ProductCategory>()
.HasOne(p => p.Category)
.WithMany(d => d.ProductCategories)
.HasForeignKey(p => p.CategoryId)
.OnDelete(DeleteBehavior.Restrict); // <-- Here it is
现在再次生成迁移并更新数据库。