简体   繁体   English

MVC 4实体框架同一表的两个外键导致循环或多个级联路径

[英]MVC 4 Entity Framework Two Foreign Keys to the Same Table Cause cycles or multiple cascade paths

I have 2 entities Unit and UnitPerUnit . 我有2个实体UnitUnitPerUnit Their roles are: 他们的角色是:

  • Unit : defines units like kg, meter, centimeter, yard, etc... Unit :定义单位,例如公斤,米,厘米,码等。
  • UnitPerUnit : holds the value between units (ex: 1kg = 1000gr) UnitPerUnit :保存单位之间的值(例如:1kg = 1000gr)

Here is the code: 这是代码:

[Table("EA_Unit", Schema = "EAccounting")]
public class EA_Unit
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    [Display(Name = "Unit Id")]
    public int UnitId { get; set; }

    [Display(Name = "Unit Name")]
    [Index("UnitName", IsUnique = true)]
    [MaxLength(20)]
    [Required]
    public string UnitName { get; set; } //Example kg, piece, roll, yard, meter

    public EA_Unit()
    {
        UnitName = "";
    }
}

[Table("EA_UnitPerUnit", Schema = "EAccounting")]
public class EA_UnitPerUnit
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    [Display(Name = "Unit Per Unit Id")]
    public int UnitPerUnitId { get; set; }

    [Display(Name = "From Unit")]
    [Required]
    [Index("UnitToUnit", 1, IsUnique = true)]
    public int UnitId { get; set; }

    [Display(Name = "To Name")]
    [Required]
    [Index("UnitToUnit", 2, IsUnique = true)]
    public int UnitToUnit { get; set; } //The comparer unit

    [Display(Name = "Amount")]
    [Required]
    public float UnitAmount { get; set; } //how much is this unit to another unit (ex: 1kg = 1000gr)

    [ForeignKey("UnitId")]
    public virtual EA_Unit Unit { get; set; }

    [ForeignKey("UnitToUnit")]
    public virtual EA_Unit UnitTo { get; set; }

    public EA_UnitPerUnit()
    {
        UnitId = 0;
        UnitToUnit = 0;
        UnitAmount = 0;
    }
}

Whenever I run the program and created the database, there is this error : 每当我运行程序并创建数据库时,就会出现此错误:

Introducing FOREIGN KEY constraint 'FK_EAccounting.EA_UnitPerUnit_EAccounting.EA_Unit_UnitToUnit' on table 'EA_UnitPerUnit' may cause cycles or multiple cascade paths. 在表'EA_UnitPerUnit'上引入FOREIGN KEY约束'FK_EAccounting.EA_UnitPerUnit_EAccounting.EA_Unit_UnitToUnit'可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. 指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。 Could not create constraint. 无法创建约束。 See previous errors. 请参阅先前的错误。

What I want is if the Unit is deleted, the UnitPerUnit entries that hold the value of that deleted Unit in either public virtual EA_Unit Unit { get; set; } 我想要的是,如果删除了Unit ,则在UnitPerUnit public virtual EA_Unit Unit { get; set; }中保存该已删除Unit的值的UnitPerUnit条目public virtual EA_Unit Unit { get; set; } public virtual EA_Unit Unit { get; set; } public virtual EA_Unit Unit { get; set; } or public virtual EA_Unit UnitTo { get; set; } public virtual EA_Unit Unit { get; set; }public virtual EA_Unit UnitTo { get; set; } public virtual EA_Unit UnitTo { get; set; } public virtual EA_Unit UnitTo { get; set; } will also be deleted. public virtual EA_Unit UnitTo { get; set; }也将被删除。

How can I overcome this problem? 我该如何克服这个问题? I want to setup the database as such the database will automatically delete the UnitPerUnit entry after the Unit entry is deleted. 我想设置数据库这样的数据库会自动删除UnitPerUnit后进入Unit条目被删除。

This issue has been resolved again and again. 此问题已得到一次又一次的解决。 Please refer to this answer . 请参考此答案

A foreign key with cascade delete means that if a record in the parent table is deleted, then the corresponding records in the child table will automatically be deleted. 具有级联删除功能的外键意味着,如果删除父表中的记录,则子表中的相应记录将被自动删除。 This is called a cascade delete in SQL Server. 这在SQL Server中称为级联删除。

Reference link . 参考链接

You will need to use the model builder to resolve this. 您将需要使用模型构建器来解决此问题。

modelBuilder.Entity<...>()
            .HasRequired(...)
            .WithMany(...)
            .HasForeignKey(...)
            .WillCascadeOnDelete(false);

or 要么

modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();   

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架C#在表Y中引入外键X可能会导致循环或多个级联路径 - Entity Framework C# Introducing A Foreign Key X In Table Y May Cause Cycles OR Multiple Cascade Paths 实体框架:在表 '' 上引入 FOREIGN KEY 约束 '' 可能会导致循环或多个级联路径 - Entity Framework: Introducing FOREIGN KEY constraint '' on table '' may cause cycles or multiple cascade paths SQL 错误:引入 FOREIGN KEY 约束可能会导致循环或多个级联路径。 实体框架核心 - SQL Error: Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths. Entity Framework Core 实体框架,外键约束可能会导致循环或多个级联路径 - Entity Framework, Foreign key constraint may cause cycles or multiple cascade paths 实体框架代码优先外键可能会导致循环或多个级联路径 - Entity Framework Code first Foreign Key may cause cycles or multiple cascade paths 引入 FOREIGN KEY 约束...可能导致循环或多个级联路径 - 实体框架 - Introducing FOREIGN KEY constraint ... may cause cycles or multiple cascade paths - Entity Framework Entity Framework Core:可能导致循环或多个级联路径 - Entity Framework Core: may cause cycles or multiple cascade paths 外键约束可能导致循环或多个级联路径 ASP.NET Core MVC - Foreign key constraint may cause cycles or multiple cascade paths ASP.NET Core MVC 外键可能会导致循环或多个级联路径 - Foreign key may cause cycles or multiple cascade paths EF FOREIGN KEY约束可能会导致循环或多个级联路径 - EF FOREIGN KEY constraint may cause cycles or multiple cascade paths
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM