Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why?

前端 未结 18 922
天涯浪人
天涯浪人 2020-11-22 02:37

I\'ve been wrestling with this for a while and can\'t quite figure out what\'s happening. I have a Card entity which contains Sides (usually 2) - and both Cards and Sides h

相关标签:
18条回答
  • 2020-11-22 03:11

    Because Stage is required, all one-to-many relationships where Stage is involved will have cascading delete enabled by default. It means, if you delete a Stage entity

    • the delete will cascade directly to Side
    • the delete will cascade directly to Card and because Card and Side have a required one-to-many relationship with cascading delete enabled by default again it will then cascade from Card to Side

    So, you have two cascading delete paths from Stage to Side - which causes the exception.

    You must either make the Stage optional in at least one of the entities (i.e. remove the [Required] attribute from the Stage properties) or disable cascading delete with Fluent API (not possible with data annotations):

    modelBuilder.Entity<Card>()
        .HasRequired(c => c.Stage)
        .WithMany()
        .WillCascadeOnDelete(false);
    
    modelBuilder.Entity<Side>()
        .HasRequired(s => s.Stage)
        .WithMany()
        .WillCascadeOnDelete(false);
    
    0 讨论(0)
  • 2020-11-22 03:12

    Just for documentation purpose, to someone that comes on the future, this thing can be solved as simple as this, and with this method, you could do a method that disabled one time, and you could access your method normally

    Add this method to the context database class:

    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    }
    
    0 讨论(0)
  • 2020-11-22 03:14

    I had this issue also, I solved it instantly with this answer from a similar thread

    In my case, I didn't want to delete the dependent record on key deletion. If this is the case in your situation just simply change the Boolean value in the migration to false:

    AddForeignKey("dbo.Stories", "StatusId", "dbo.Status", "StatusID", cascadeDelete: false);
    

    Chances are, if you are creating relationships which throw this compiler error but DO want to maintain cascade delete; you have an issue with your relationships.

    0 讨论(0)
  • 2020-11-22 03:15

    You can set cascadeDelete to false or true (in your migration Up() method). Depends upon your requirement.

    AddForeignKey("dbo.Stories", "StatusId", "dbo.Status", "StatusID", cascadeDelete: false);
    
    0 讨论(0)
  • 2020-11-22 03:15

    The existing answers are great I just wanted to add that I ran into this error because of a different reason. I wanted to create an Initial EF migration on an existing DB but I didn't use the -IgnoreChanges flag and applied the Update-Database command on an empty Database (also on the existing fails).

    Instead I had to run this command when the current db structure is the current one:

    Add-Migration Initial -IgnoreChanges
    

    There is likely a real problem in the db structure but save the world one step at a time...

    0 讨论(0)
  • This sounds weird and I don't know why, but in my case that was happening because my ConnectionString was using "." in "data source" attribute. Once I changed it to "localhost" it workded like a charm. No other change was needed.

    0 讨论(0)
提交回复
热议问题