Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On Delete Cascade is applied to all tables automatically #4213

Closed
bastienJS opened this issue Jan 4, 2016 · 2 comments
Closed

On Delete Cascade is applied to all tables automatically #4213

bastienJS opened this issue Jan 4, 2016 · 2 comments

Comments

@bastienJS
Copy link

When I do this in EF 7 RC1

   builder.Entity<Schoolclass>()
                        .HasMany(x => x.Pupils)
                        .WithOne(x => x.Schoolclass)
                        .OnDelete(Microsoft.Data.Entity.Metadata.DeleteBehavior.Cascade);

I just change On Delete Cascade for ONE table.

When I look at the migration code, all tables Referential Integrity is changed ???

from ReferentialAction.Restrict to Cascade !!!

using System;
using System.Collections.Generic;
using Microsoft.Data.Entity.Migrations;

namespace TGB.DataProvider.Migrations
{
    public partial class DeleteONCascadeforPupil : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(name: "FK_Pupil_Schoolclass_SchoolclassId", table: "Pupil");
            migrationBuilder.DropForeignKey(name: "FK_Schoolclass_Schoolyear_SchoolyearId", table: "Schoolclass");
            migrationBuilder.DropForeignKey(name: "FK_SchoolclassTest_Schoolclass_SchoolclassId", table: "SchoolclassTest");
            migrationBuilder.DropForeignKey(name: "FK_SchoolclassTest_Test_TestId", table: "SchoolclassTest");
            migrationBuilder.DropForeignKey(name: "FK_SubjectPupil_Pupil_PupilId", table: "SubjectPupil");
            migrationBuilder.DropForeignKey(name: "FK_SubjectPupil_Subject_SubjectId", table: "SubjectPupil");
            migrationBuilder.DropForeignKey(name: "FK_SubjectTest_Subject_SubjectId", table: "SubjectTest");
            migrationBuilder.DropForeignKey(name: "FK_SubjectTest_Test_TestId", table: "SubjectTest");
            migrationBuilder.DropForeignKey(name: "FK_TestType_Schoolyear_SchoolyearId", table: "TestType");
            migrationBuilder.DropForeignKey(name: "FK_TestTypeTest_Test_TestId", table: "TestTypeTest");
            migrationBuilder.DropForeignKey(name: "FK_TestTypeTest_TestType_TestTypeId", table: "TestTypeTest");
            migrationBuilder.AddForeignKey(
                name: "FK_Pupil_Schoolclass_SchoolclassId",
                table: "Pupil",
                column: "SchoolclassId",
                principalTable: "Schoolclass",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
            migrationBuilder.AddForeignKey(
                name: "FK_Schoolclass_Schoolyear_SchoolyearId",
                table: "Schoolclass",
                column: "SchoolyearId",
                principalTable: "Schoolyear",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
            migrationBuilder.AddForeignKey(
                name: "FK_SchoolclassTest_Schoolclass_SchoolclassId",
                table: "SchoolclassTest",
                column: "SchoolclassId",
                principalTable: "Schoolclass",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
            migrationBuilder.AddForeignKey(
                name: "FK_SchoolclassTest_Test_TestId",
                table: "SchoolclassTest",
                column: "TestId",
                principalTable: "Test",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
            migrationBuilder.AddForeignKey(
                name: "FK_SubjectPupil_Pupil_PupilId",
                table: "SubjectPupil",
                column: "PupilId",
                principalTable: "Pupil",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
            migrationBuilder.AddForeignKey(
                name: "FK_SubjectPupil_Subject_SubjectId",
                table: "SubjectPupil",
                column: "SubjectId",
                principalTable: "Subject",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
            migrationBuilder.AddForeignKey(
                name: "FK_SubjectTest_Subject_SubjectId",
                table: "SubjectTest",
                column: "SubjectId",
                principalTable: "Subject",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
            migrationBuilder.AddForeignKey(
                name: "FK_SubjectTest_Test_TestId",
                table: "SubjectTest",
                column: "TestId",
                principalTable: "Test",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
            migrationBuilder.AddForeignKey(
                name: "FK_TestType_Schoolyear_SchoolyearId",
                table: "TestType",
                column: "SchoolyearId",
                principalTable: "Schoolyear",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
            migrationBuilder.AddForeignKey(
                name: "FK_TestTypeTest_Test_TestId",
                table: "TestTypeTest",
                column: "TestId",
                principalTable: "Test",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
            migrationBuilder.AddForeignKey(
                name: "FK_TestTypeTest_TestType_TestTypeId",
                table: "TestTypeTest",
                column: "TestTypeId",
                principalTable: "TestType",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(name: "FK_Pupil_Schoolclass_SchoolclassId", table: "Pupil");
            migrationBuilder.DropForeignKey(name: "FK_Schoolclass_Schoolyear_SchoolyearId", table: "Schoolclass");
            migrationBuilder.DropForeignKey(name: "FK_SchoolclassTest_Schoolclass_SchoolclassId", table: "SchoolclassTest");
            migrationBuilder.DropForeignKey(name: "FK_SchoolclassTest_Test_TestId", table: "SchoolclassTest");
            migrationBuilder.DropForeignKey(name: "FK_SubjectPupil_Pupil_PupilId", table: "SubjectPupil");
            migrationBuilder.DropForeignKey(name: "FK_SubjectPupil_Subject_SubjectId", table: "SubjectPupil");
            migrationBuilder.DropForeignKey(name: "FK_SubjectTest_Subject_SubjectId", table: "SubjectTest");
            migrationBuilder.DropForeignKey(name: "FK_SubjectTest_Test_TestId", table: "SubjectTest");
            migrationBuilder.DropForeignKey(name: "FK_TestType_Schoolyear_SchoolyearId", table: "TestType");
            migrationBuilder.DropForeignKey(name: "FK_TestTypeTest_Test_TestId", table: "TestTypeTest");
            migrationBuilder.DropForeignKey(name: "FK_TestTypeTest_TestType_TestTypeId", table: "TestTypeTest");
            migrationBuilder.AddForeignKey(
                name: "FK_Pupil_Schoolclass_SchoolclassId",
                table: "Pupil",
                column: "SchoolclassId",
                principalTable: "Schoolclass",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
            migrationBuilder.AddForeignKey(
                name: "FK_Schoolclass_Schoolyear_SchoolyearId",
                table: "Schoolclass",
                column: "SchoolyearId",
                principalTable: "Schoolyear",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
            migrationBuilder.AddForeignKey(
                name: "FK_SchoolclassTest_Schoolclass_SchoolclassId",
                table: "SchoolclassTest",
                column: "SchoolclassId",
                principalTable: "Schoolclass",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
            migrationBuilder.AddForeignKey(
                name: "FK_SchoolclassTest_Test_TestId",
                table: "SchoolclassTest",
                column: "TestId",
                principalTable: "Test",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
            migrationBuilder.AddForeignKey(
                name: "FK_SubjectPupil_Pupil_PupilId",
                table: "SubjectPupil",
                column: "PupilId",
                principalTable: "Pupil",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
            migrationBuilder.AddForeignKey(
                name: "FK_SubjectPupil_Subject_SubjectId",
                table: "SubjectPupil",
                column: "SubjectId",
                principalTable: "Subject",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
            migrationBuilder.AddForeignKey(
                name: "FK_SubjectTest_Subject_SubjectId",
                table: "SubjectTest",
                column: "SubjectId",
                principalTable: "Subject",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
            migrationBuilder.AddForeignKey(
                name: "FK_SubjectTest_Test_TestId",
                table: "SubjectTest",
                column: "TestId",
                principalTable: "Test",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
            migrationBuilder.AddForeignKey(
                name: "FK_TestType_Schoolyear_SchoolyearId",
                table: "TestType",
                column: "SchoolyearId",
                principalTable: "Schoolyear",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
            migrationBuilder.AddForeignKey(
                name: "FK_TestTypeTest_Test_TestId",
                table: "TestTypeTest",
                column: "TestId",
                principalTable: "Test",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
            migrationBuilder.AddForeignKey(
                name: "FK_TestTypeTest_TestType_TestTypeId",
                table: "TestTypeTest",
                column: "TestTypeId",
                principalTable: "TestType",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
        }
    }
}
@rowanmiller
Copy link
Contributor

Starting in RC1 - cascade delete is on by default for all required relationships (this is the same behavior as EF6 and we were just waiting for cascade delete to be implemented to enable it). After aligning the schema with the new convention, you may still hit issue #3751 where migrations will continually try to drop and re-create foreign keys because it thinks the cascade behavior has changed. This is fixed in our current code base (and will ship in RC2).

@bastienJS
Copy link
Author

Thanks cant wait for RC2!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants