【问题标题】:"Introducing FOREIGN KEY constraint" error“引入 FOREIGN KEY 约束”错误
【发布时间】:2019-10-05 11:20:48
【问题描述】:

我有 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 约束。
无法创建约束或索引。查看以前的错误。

我无法识别这里的问题,我做错了什么?

我的ProductCategory 实体不应该有HasOne.WithMany 代码或类似的东西吗?

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

【问题讨论】:

    标签: c# entity-framework-core


    【解决方案1】:

    在您的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
    

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

    【讨论】:

    • 感谢帮助,原来是“WillCascadeOnDelete()”,后来改成了“OnDelete()”,我不知道,再次感谢。
    猜你喜欢
    • 2014-03-20
    • 2018-08-08
    • 2013-07-23
    • 1970-01-01
    • 2016-11-04
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多