简体   繁体   中英

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

I have 2 entities Unit and UnitPerUnit . Their roles are:

  • Unit : defines units like kg, meter, centimeter, yard, etc...
  • UnitPerUnit : holds the value between units (ex: 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. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. 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; } 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 UnitTo { get; set; } public virtual EA_Unit UnitTo { get; set; } will also be deleted.

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.

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.

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>();   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM