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

前端 未结 18 950
天涯浪人
天涯浪人 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:02

    I was getting this error for lots of entities when I was migrating down from an EF7 model to an EF6 version. I didn't want to have to go through each entity one at a time, so I used:

    builder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
    builder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    
    0 讨论(0)
  • 2020-11-22 03:03

    I had a table that had a circular relationship with others and i was getting the same error. Turns out it is about the foreign key which was not nullable. If key is not nullable related object must be deleted and circular relations doesnt allow that. So use nullable foreign key.

    [ForeignKey("StageId")]
    public virtual Stage Stage { get; set; }
    public int? StageId { get; set; }
    
    0 讨论(0)
  • 2020-11-22 03:04

    Here's what I did. This will apply to all Circular relationships, hence you don't have to specify one by one inside OnModelCreating.

    Steps

    1. Remove last migration (dotnet ef migrations remove)
    2. Add the following code snippet in your OnModelCreating

    var cascadeFKs = modelBuilder.Model.GetEntityTypes()
        .SelectMany(t => t.GetForeignKeys())
        .Where(fk => !fk.IsOwnership && fk.DeleteBehavior == DeleteBehavior.Cascade);
    
    foreach (var fk in cascadeFKs)
        fk.DeleteBehavior = DeleteBehavior.Restrict;

    1. Create new migrations
    2. Update database

    This did the trick for me.

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

    I fixed this. When you add the migration, in the Up() method there will be a line like this:

    .ForeignKey("dbo.Members", t => t.MemberId, cascadeDelete:True)
    

    If you just delete the cascadeDelete from the end it will work.

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

    I ran into the same problem and stuck for a long. The following steps saved me. Go through the constraints and change the onDelete ReferentialAction to NoAction from Cascade

      constraints: table =>
      {
          table.PrimaryKey("PK_table1", x => x.Id);
          table.ForeignKey(
             name: "FK_table1_table2_table2Id",
             column: x => x.table2Id,
             principalTable: "table2",
             principalColumn: "Id",
             onDelete: ReferentialAction.NoAction);
      });
    
    0 讨论(0)
  • 2020-11-22 03:08

    None of the aforementioned solutions worked for me. What I had to do was use a nullable int (int?) on the foreign key that was not required (or not a not null column key) and then delete some of my migrations.

    Start by deleting the migrations, then try the nullable int.

    Problem was both a modification and model design. No code change was necessary.

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