[英]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个实体Unit
和UnitPerUnit
。 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中称为级联删除。
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.