OnDelete(DeleteBehavior.Cascade) 可能导致循环或多个级联路径
OnDelete(DeleteBehavior.Cascade) may cause cycles or multiple cascade paths on
我有一个产品 table 和一个类别 table。我试图在产品和类别之间建立多对多关系。所以我有一个 Table 叫做:ProductCategories - 我遵循了官方文档:
public class ProductCategory
{
public Guid ProductId { get; set; }
public Product Product { get; set; }
public Guid CategoryId { get; set; }
public Category Category { get; set; }
}
public class Product
{
public Product()
{
ProductFiles = new Collection<ProductFiles>();
}
public Company Company { get; set; }
public Guid CompanyId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal SalePrice { get; set; }
public decimal? CostPrice { get; set; }
public int VatPercentage { get; set; } = 25;
public ICollection<ProductFiles> ProductFiles { get; set; }
public ICollection<ProductCategory> Categories { get; set; }
}
public class Category
{
public string Title { get; set; }
public Guid CompanyId { get; set; }
public Company Company { get; set; }
public Guid? ParentCategoryId { get; set; }
public virtual ICollection<Category> SubCategories { get; set; }
public virtual Category ParentCategory { get; set; }
public bool Visible { get; set; } = true;
public int SortOrder { get; set; } = 1;
public ICollection<ProductCategory> Products { get; set; }
}
并且在模型构建器中我指定了关系
builder.Entity<Company>()
.HasMany(c => c.Products)
.WithOne(e => e.Company);
builder.Entity<Product>()
.Property(p => p.CostPrice)
.HasColumnType("decimal(18,2)");
builder.Entity<Product>()
.Property(p => p.SalePrice)
.HasColumnType("decimal(18,2)");
builder.Entity<Category>()
.HasMany<Category>(c => c.SubCategories)
.WithOne(c => c.ParentCategory)
.HasForeignKey(c => c.ParentCategoryId)
.OnDelete(DeleteBehavior.Restrict);
builder.Entity<Company>()
.HasMany<Category>(p => p.Categories)
.WithOne(p => p.Company)
.HasForeignKey(p => p.CompanyId).OnDelete(DeleteBehavior.Cascade);
builder.Entity()
.HasKey(x => new {x.ProductId, x.CategoryId});
builder.Entity<ProductCategory>()
.HasOne<Product>(pc => pc.Product)
.WithMany(p => p.Categories)
.HasForeignKey(p => p.ProductId);
builder.Entity<ProductCategory>()
.HasOne<Category>(p => p.Category)
.WithMany(p => p.Products)
.HasForeignKey(p => p.CategoryId);
问题是当我尝试更新数据库时出现错误“可能导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。
无法创建约束或索引。"
我想要的是当系统中有人删除一个category/product时,它应该删除ProductCategories中的记录 - 当我尝试添加onDelete时没有动作,那么我无法删除category/product 在 ProductsCategory table.
中没有它们的关系
关于如何最好地解决这个问题有什么建议吗?
公司配置为级联删除类别和产品。那是“多级联路径”,是不允许的。将所有 FK 配置放在一起,可以更轻松地查看哪些关系可以级联。例如
builder.Entity<Company>()
.HasMany(c => c.Products)
.WithOne(e => e.Company)
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<Company>()
.HasMany(p => p.Categories)
.WithOne(p => p.Company)
.HasForeignKey(p => p.CompanyId)
.OnDelete(DeleteBehavior.Restrict);
builder.Entity<Category>()
.HasMany(c => c.SubCategories)
.WithOne(c => c.ParentCategory)
.HasForeignKey(c => c.ParentCategoryId)
.OnDelete(DeleteBehavior.Restrict);
builder.Entity<ProductCategory>()
.HasOne(pc => pc.Product)
.WithMany(p => p.Categories)
.HasForeignKey(p => p.ProductId)
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<ProductCategory>()
.HasOne(p => p.Category)
.WithMany(p => p.Products)
.HasForeignKey(p => p.CategoryId)
.OnDelete(DeleteBehavior.Cascade);
我有一个产品 table 和一个类别 table。我试图在产品和类别之间建立多对多关系。所以我有一个 Table 叫做:ProductCategories - 我遵循了官方文档:
public class ProductCategory
{
public Guid ProductId { get; set; }
public Product Product { get; set; }
public Guid CategoryId { get; set; }
public Category Category { get; set; }
}
public class Product
{
public Product()
{
ProductFiles = new Collection<ProductFiles>();
}
public Company Company { get; set; }
public Guid CompanyId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal SalePrice { get; set; }
public decimal? CostPrice { get; set; }
public int VatPercentage { get; set; } = 25;
public ICollection<ProductFiles> ProductFiles { get; set; }
public ICollection<ProductCategory> Categories { get; set; }
}
public class Category
{
public string Title { get; set; }
public Guid CompanyId { get; set; }
public Company Company { get; set; }
public Guid? ParentCategoryId { get; set; }
public virtual ICollection<Category> SubCategories { get; set; }
public virtual Category ParentCategory { get; set; }
public bool Visible { get; set; } = true;
public int SortOrder { get; set; } = 1;
public ICollection<ProductCategory> Products { get; set; }
}
并且在模型构建器中我指定了关系
builder.Entity<Company>()
.HasMany(c => c.Products)
.WithOne(e => e.Company);
builder.Entity<Product>()
.Property(p => p.CostPrice)
.HasColumnType("decimal(18,2)");
builder.Entity<Product>()
.Property(p => p.SalePrice)
.HasColumnType("decimal(18,2)");
builder.Entity<Category>()
.HasMany<Category>(c => c.SubCategories)
.WithOne(c => c.ParentCategory)
.HasForeignKey(c => c.ParentCategoryId)
.OnDelete(DeleteBehavior.Restrict);
builder.Entity<Company>()
.HasMany<Category>(p => p.Categories)
.WithOne(p => p.Company)
.HasForeignKey(p => p.CompanyId).OnDelete(DeleteBehavior.Cascade);
builder.Entity() .HasKey(x => new {x.ProductId, x.CategoryId});
builder.Entity<ProductCategory>()
.HasOne<Product>(pc => pc.Product)
.WithMany(p => p.Categories)
.HasForeignKey(p => p.ProductId);
builder.Entity<ProductCategory>()
.HasOne<Category>(p => p.Category)
.WithMany(p => p.Products)
.HasForeignKey(p => p.CategoryId);
问题是当我尝试更新数据库时出现错误“可能导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。 无法创建约束或索引。"
我想要的是当系统中有人删除一个category/product时,它应该删除ProductCategories中的记录 - 当我尝试添加onDelete时没有动作,那么我无法删除category/product 在 ProductsCategory table.
中没有它们的关系关于如何最好地解决这个问题有什么建议吗?
公司配置为级联删除类别和产品。那是“多级联路径”,是不允许的。将所有 FK 配置放在一起,可以更轻松地查看哪些关系可以级联。例如
builder.Entity<Company>()
.HasMany(c => c.Products)
.WithOne(e => e.Company)
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<Company>()
.HasMany(p => p.Categories)
.WithOne(p => p.Company)
.HasForeignKey(p => p.CompanyId)
.OnDelete(DeleteBehavior.Restrict);
builder.Entity<Category>()
.HasMany(c => c.SubCategories)
.WithOne(c => c.ParentCategory)
.HasForeignKey(c => c.ParentCategoryId)
.OnDelete(DeleteBehavior.Restrict);
builder.Entity<ProductCategory>()
.HasOne(pc => pc.Product)
.WithMany(p => p.Categories)
.HasForeignKey(p => p.ProductId)
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<ProductCategory>()
.HasOne(p => p.Category)
.WithMany(p => p.Products)
.HasForeignKey(p => p.CategoryId)
.OnDelete(DeleteBehavior.Cascade);