Setting the On Update Cascade Constraint with Entity Framework Core
Asked Answered
E

2

14

There is a ton of info on setting the behavior for delete actions for foreign key relationships in Entity Framework Core, however, I have found nearly zero details on how to specify the "On Update Cascade" constraint of a foreign key.

The closest I have found is this migrations related Microsoft document.

public void Configure(EntityTypeBuilder<Something> builder)
{
    builder
        .HasOne(s => s.Thing)
        .WithMany(t => t.Somethings)
        .HasForeignKey(s => s.ThingId)
        --> Like Delete behavior, how to set update behavior?
        .OnDelete(DeleteBehavior.Cascade);
}

How can this be done with the Fluent API?

Electrokinetics answered 1/8, 2018 at 19:40 Comment(2)
You won't find such info because simply there is no such functionality (not supported neither by EF6 nor EF Core). It doesn't make sense since EF (Core) does not allow modifying the PK. All you can do is to use DeleteBehavior.SetNull for optional relationships.Debug
Yep, you can set it in a migration: ForeignKey(onUpdate: ReferentialAction.Cascade) But yeah, it's only useful for updates made outside of EF.Mullion
B
12

Update: This still does not fix the underlying issue of when you "context.SaveChanges();" it will still throw an error. You have to null the record in the Database and then repopulate it.

I have been looking for the Exact same thing and I found a work around. As far as I can tell you cannot do this in the Fluent API yet. What you can do is add it into the migration manually.

  1. Add Migration
  2. Open Migration
  3. Find the "onDelete: ReferentialAction.Cascade);"
  4. On the line above it, Insert "onUpdate: ReferentialAction.Cascade,"
  5. Update and test database
  6. See below for reference

            migrationBuilder.CreateTable(
            name: "AgencyMembers",
            columns: table => new
            {
                ApplicationUserId = table.Column<string>(maxLength: 450, nullable: false),
                AgencyId = table.Column<int>(nullable: false),
                AgencyName = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_AgencyMembers", x => new { x.ApplicationUserId, x.AgencyId });
                table.ForeignKey(
                    name: "FK_AgencyMembers_AspNetUsers_ApplicationUserId",
                    column: x => x.ApplicationUserId,
                    principalTable: "AspNetUsers",
                    principalColumn: "Id",
                    ***onUpdate: ReferentialAction.Cascade,***
                    onDelete: ReferentialAction.Cascade);
                table.ForeignKey(
                    name: "FK_AgencyMembers_Agencies_AgencyId_AgencyName",
                    columns: x => new { x.AgencyId, x.AgencyName },
                    principalTable: "Agencies",
                    principalColumns: new[] { "AgencyId", "AgencyName" },
                    ***onUpdate: ReferentialAction.Cascade,***
                    onDelete: ReferentialAction.Cascade);
            });
    
Birl answered 15/11, 2018 at 20:40 Comment(1)
This is great, but the next time you create a migration it will try to drop and recreate the foreign key with onDelete only. Has anyone found a workaround to prevent that?Scherer
R
0

Even you update your table as cascade update, since Ef core doenst support the cascade update, you should execute raw sql on DbContext so that cascade update all references

Radiosurgery answered 16/2 at 11:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.