"Introducing FOREIGN KEY constraint"错误



我有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上引入外键约束" FK_ProductCategory_products_productid"可能会导致周期或多个级联路径。在删除no操作或更新否操作上指定或修改其他外键约束。
无法创建约束或索引。请参阅以前的错误。

我在这里无法识别这个问题,我在做什么错?

我不应该将HasOne.WithMany代码与我的ProductCategory实体一样?

我几乎尝试了所有内容,但找不到解决方案。

在您的ProductCategory 流利API 配置中指定.OnDelete(DeleteBehavior.Restrict)如下:

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

现在再次生成迁移并更新数据库。

相关内容

最新更新