Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths

asp.net-mvc entity-framework-6

Question

The following entities are part of my MVC project (code first approach) I would like to know why I am experiencing the following error.

Introducing FOREIGN KEY constraint 'FK_dbo.VendorDetails_dbo.States_StateID' on table 'VendorDetails' 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.

I just needed to know what tables are receiving numerous cascade paths, what diagrams will work, and what to use the fluent API for.modelBuilder.Entity<...>() .HasRequired(...) .WithMany(...) .HasForeignKey(...) .WillCascadeOnDelete(false);

public class VendorDetails
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int VendorID { get; set; }        
    [MaxLength(60)]
    public string VendorName { get; set; }        

    public int VendorTypeID { get; set; }
    public int CountryID { get; set; }
    public int StateID { get; set; }        

    [NotMapped]
    public string CountryName { get; set; }
    [NotMapped]
    public string StateName { get; set; }
    [NotMapped]
    public string VendorTypeName { get; set; }

    public virtual Country Country { get; set; }
    public virtual State State { get; set; }
    public virtual VendorType VendorType { get; set; }
}

public class VendorType
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int VendorTypeID { get; set; }                

    public string VendorTypeName { get; set; }

    public virtual ICollection<VendorDetails> Vendors { get; set; }
 }


public class Country
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CountryID { get; set; }
    public string CountryName { get; set; }

    public virtual ICollection<State> States { get; set; }
}

public class State
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int StateID { get; set; }
    public string StateName { get; set; }

    public int CountryID { get; set; }
    public virtual Country Country { get; set; }
}
1
2
4/29/2015 8:10:12 AM

Accepted Answer

You're seeing this because VendorDetails contains links to both Country and State, and State has links to both Country and VendorDetails. This provides two direct paths as well as one through Country between VendorDetails and State.

Cascade delete on the link to the vendor details should be disabled, in my opinion.

modelBuilder
    .Entity<VendorDetails>()
        .HasOptional(e => e.State)
        .WithMany()
        .WillCascadeOnDelete(false);
2
4/30/2015 7:31:53 AM

Popular Answer

As an alternative to @Richard's advice, the migration may have "onDelete" specified when it is created in the restrictions section.

i.e.

table.ForeignKey( name: "FK_ServiceRequest_User_DeletedById",
                  column: x => x.DeletedById,
                  principalTable: "User",
                  principalColumn: "Id",
                  onDelete: ReferentialAction.Cascade);

Simply strike out the final line of this paragraph (providing your default is to perform no action). Unless otherwise stated, setting theonDelete to ReferentialAction.NoAction yields the same outcome.

Although this is an alternative that keeps the migration as "self managing," I personally prefer @Richard's method because it consolidates the post processing in one location (the context) and eliminates the need to search through what could be a large number of migrations when looking for items of this nature.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow