0

在插入此代码时,我在代码优先 MVC CORE 中收到此错误,并且我正在为第一个 Time 命令执行代码优先:

Update-Database -Context ProjectDbContext

“在表 'Product' 上引入 FOREIGN KEY 约束 'FK_Product_SubCategory_SubCategoryId' 可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束”。

下面给出了我的类,并解释了为什么我们在 mvc 模型类中使用关联,如下所示:

public virtual SubCategory SubCategory { get; set; }

Or
public virtual ICollection<Category> Category { get; set; }

public virtual Icollection<SubCategory> SubCategory { get; set; }



public class Product
{

    public int ProductId { get; set; }

    [Required]
    public string ProductName { get; set; }

    [Required]
    public string ProductModel { get; set; }

    [Required]
    public int Quatity { get; set; }

    [Required]
    public int Price { get; set; }

    [Required]
    public string Description { get; set; }

    [Required]
    public string ProductColor { get; set; }

    public byte Status { get; set; }

    public DateTime TodayDate { get; set; }

    [Required]
    public int CategoryId { get; set; }

    [Required]
    public int SubCategoryId { get; set; }

    public virtual Category Category { get; set; }
    public virtual SubCategory SubCategory { get; set; }
}

public class SubCategory
{
    public int SubCategoryId { get; set; }

    [Required]
    public string SubCategoryName { set; get; }

    [Required]
    public int CategoryId { get; set; }

    public ICollection< Category> Category { set; get; }

    public ICollection<Product> Product { set; get; }
}  


public class Category
{
    public int CategoryId { set; get; }

    [Required]
    public string Name { get; set; }
}
4

1 回答 1

0

在 ApplicationDbContext 类下的身份模型文件中添加以下内容

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {            
        base.OnModelCreating(modelBuilder);
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    }

希望这会有所帮助,但您的级联删除将关闭。

于 2018-11-29T08:56:15.387 回答