C# &引用;引入外键约束“;错误

C# &引用;引入外键约束“;错误,c#,entity-framework-core,C#,Entity Framework Core,我有3个主要实体:类别、子类别、产品: public class Product { public Product() { Photos = new List<Photo>(); Comments = new List<Comment>(); Colors = new List<ProductColor>(); Attributes = new List<ProductAtt

我有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; }
}
当我尝试从PowerShell迁移数据库时,出现以下错误:

在表“ProductCategory”上引入外键约束“FK_ProductCategory_Products_ProductId”可能会导致循环或多个级联路径。指定“删除不操作”或“更新不操作”,或修改其他外键约束。
无法创建约束或索引。请参阅前面的错误

我没意识到这个问题,我做错了什么

我的
ProductCategory
实体或类似的东西是否应该有
HasOne.with many
代码

我尝试了几乎所有的方法,但仍然找不到解决方案。

产品类别中指定
.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
modelBuilder.Entity()
.HasOne(p=>p.Product)
.WithMany(d=>d.ProductCategories)
.HasForeignKey(p=>p.ProductId)
.OnDelete(DeleteBehavior.Restrict);//p、 (类别)
.WithMany(d=>d.ProductCategories)
.HasForeignKey(p=>p.CategoryId)
.OnDelete(DeleteBehavior.Restrict);// 在
ProductCategory
Fluent 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
modelBuilder.Entity()
.HasOne(p=>p.Product)
.WithMany(d=>d.ProductCategories)
.HasForeignKey(p=>p.ProductId)
.OnDelete(DeleteBehavior.Restrict);//p、 (类别)
.WithMany(d=>d.ProductCategories)
.HasForeignKey(p=>p.CategoryId)
.OnDelete(DeleteBehavior.Restrict);//谢谢你的帮助,它是“WillCascadeOnDelete()”,但改为“OnDelete()”,我不知道,再次感谢。谢谢你的帮助,它是“WillCascadeOnDelete()”,但改为“OnDelete()”,我不知道,再次感谢。