Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the On Update Cascade Constraint with Entity Framework Core

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?

like image 284
Max R McCarty Avatar asked Aug 01 '18 19:08

Max R McCarty


People also ask

How do I enable cascade delete in Entity Framework?

Cascade delete is enabled by default in Entity Framework for all types of relationships such as one-to-one, one-to-many and many-to-many.

Will Cascade delete Entity Framework Core?

The EF Core in-memory database does not currently support cascade deletes in the database.

What is on delete cascade and on Delete Set Default?

ON DELETE CASCADE : if a row of the referenced table is deleted, then all matching rows in the referencing table are deleted. ON DELETE SET NULL : if a row of the referenced table is deleted, then all referencing columns in all matching rows of the referencing table to be set to null.


1 Answers

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);
            });
    
like image 100
Dragon Mastery Avatar answered Oct 10 '22 23:10

Dragon Mastery