Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths

asp.net-mvc entity-framework-6

Question

I have the following entites in my MVC project (code first approach) I want to know that what's the reason that I am getting 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 wanted to know what tables are getting multiple cascade paths any diagram will be effective and what should I write using fluent API like this: 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 getting this because Country has a link to State, and VendorDetails has a link to both Country and State. This gives multiple paths between VendorDetails and State - one through Country and one direct.

I would disable cascade delete on the link to state from VendorDetails:

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

Popular Answer

Alternative to @Richard's suggestion, when the migration is created it may have 'onDelete' specified within the constraints section.

i.e.

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

You can simply remove the last line of this section (providing your default is to perform no action). Otherwise specifically setting the onDelete to ReferentialAction.NoAction has the same result.

I'd personally prefer @Richard's way of doing this as it puts the post processing in one place (the context) and means that when you are looking for things like this you aren't having to search through what could be masses of migrations, however this is an alternative that keeps the migration as 'self managing'.



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