Skip to content

Commit

Permalink
Tune mappings between ReferentialAction and DeleteBehavior
Browse files Browse the repository at this point in the history
Fixes #21252
  • Loading branch information
bricelam committed Aug 9, 2021
1 parent 762e870 commit ebf6fc9
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,10 @@ private static void AssignOnDeleteAction(
foreignKey.DeleteBehavior = DeleteBehavior.SetNull;
break;

case ReferentialAction.Restrict:
foreignKey.DeleteBehavior = DeleteBehavior.Restrict;
break;

default:
foreignKey.DeleteBehavior = DeleteBehavior.ClientSetNull;
break;
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Relational/Metadata/Internal/RelationalModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1127,8 +1127,8 @@ public static ReferentialAction ToReferentialAction(DeleteBehavior deleteBehavio
{
DeleteBehavior.SetNull => ReferentialAction.SetNull,
DeleteBehavior.Cascade => ReferentialAction.Cascade,
DeleteBehavior.NoAction or DeleteBehavior.ClientNoAction => ReferentialAction.NoAction,
DeleteBehavior.Restrict or DeleteBehavior.ClientSetNull or DeleteBehavior.ClientCascade => ReferentialAction.Restrict,
DeleteBehavior.NoAction or DeleteBehavior.ClientSetNull or DeleteBehavior.ClientCascade or DeleteBehavior.ClientNoAction => ReferentialAction.NoAction,
DeleteBehavior.Restrict => ReferentialAction.Restrict,
_ => throw new NotSupportedException(deleteBehavior.ToString()),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,7 @@ public virtual Task Add_foreign_key()
var foreignKey = ordersTable.ForeignKeys.Single();
if (AssertConstraintNames)
Assert.Equal("FK_Orders_Customers_CustomerId", foreignKey.Name);
Assert.Equal(Normalize(ReferentialAction.Restrict), foreignKey.OnDelete);
Assert.Equal(ReferentialAction.NoAction, foreignKey.OnDelete);
Assert.Same(customersTable, foreignKey.PrincipalTable);
Assert.Same(customersTable.Columns.Single(), Assert.Single(foreignKey.PrincipalColumns));
Assert.Equal("CustomerId", Assert.Single(foreignKey.Columns).Name);
Expand Down Expand Up @@ -1590,9 +1590,6 @@ protected virtual bool AssertConstraintNames

protected abstract string NonDefaultCollation { get; }

protected virtual ReferentialAction Normalize(ReferentialAction value)
=> value;

protected virtual DbContext CreateContext()
=> Fixture.CreateContext();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3172,7 +3172,7 @@ public void Add_optional_foreign_key()
Assert.Equal("dbo", addFkOperation.PrincipalSchema);
Assert.Equal("Amoeba", addFkOperation.PrincipalTable);
Assert.Equal(new[] { "Id" }, addFkOperation.PrincipalColumns);
Assert.Equal(ReferentialAction.Restrict, addFkOperation.OnDelete);
Assert.Equal(ReferentialAction.NoAction, addFkOperation.OnDelete);
Assert.Equal(ReferentialAction.NoAction, addFkOperation.OnUpdate);
});
}
Expand Down Expand Up @@ -3304,7 +3304,7 @@ public void Add_required_foreign_key_with_default()
Assert.Equal("dbo", addFkOperation.PrincipalSchema);
Assert.Equal("Amoeba", addFkOperation.PrincipalTable);
Assert.Equal(new[] { "Id" }, addFkOperation.PrincipalColumns);
Assert.Equal(ReferentialAction.Restrict, addFkOperation.OnDelete);
Assert.Equal(ReferentialAction.NoAction, addFkOperation.OnDelete);
Assert.Equal(ReferentialAction.NoAction, addFkOperation.OnUpdate);
});
}
Expand Down Expand Up @@ -3578,7 +3578,7 @@ public void Alter_foreign_key_cascade_delete()
}

[ConditionalFact]
public void Alter_foreign_key_on_delete_from_ClientSetNull_to_Restrict()
public void Alter_foreign_key_on_delete_from_ClientSetNull_to_NoAction()
{
Execute(
source => source.Entity(
Expand All @@ -3598,7 +3598,7 @@ public void Alter_foreign_key_on_delete_from_ClientSetNull_to_Restrict()
x.ToTable("Mushroom", "dbo");
x.Property<int>("Id");
x.Property<int>("ParentId1");
x.HasOne("Mushroom").WithMany().HasForeignKey("ParentId1").OnDelete(DeleteBehavior.Restrict);
x.HasOne("Mushroom").WithMany().HasForeignKey("ParentId1").OnDelete(DeleteBehavior.NoAction);
x.Property<int>("ParentId2");
}),
operations => Assert.Equal(0, operations.Count));
Expand Down Expand Up @@ -5600,7 +5600,7 @@ public void Change_TPH_to_TPT_with_FKs_and_seed_data()
Assert.Equal("Animal", fk.PrincipalTable);
Assert.Equal(new[] { "PreyId" }, fk.Columns);
Assert.Equal(new[] { "Id" }, fk.PrincipalColumns);
Assert.Equal(ReferentialAction.Restrict, fk.OnDelete);
Assert.Equal(ReferentialAction.NoAction, fk.OnDelete);
});
Assert.Empty(operation.UniqueConstraints);
Expand Down Expand Up @@ -5767,7 +5767,7 @@ public void Change_TPH_to_TPT_with_FKs_and_seed_data()
Assert.Equal("Mice", operation.PrincipalTable);
Assert.Equal(new[] { "MouseId" }, operation.Columns);
Assert.Equal(new[] { "Id" }, operation.PrincipalColumns);
Assert.Equal(ReferentialAction.Restrict, operation.OnDelete);
Assert.Equal(ReferentialAction.NoAction, operation.OnDelete);
},
o =>
{
Expand All @@ -5787,7 +5787,7 @@ public void Change_TPH_to_TPT_with_FKs_and_seed_data()
Assert.Equal("Animal", operation.PrincipalTable);
Assert.Equal(new[] { "PreyId" }, operation.Columns);
Assert.Equal(new[] { "Id" }, operation.PrincipalColumns);
Assert.Equal(ReferentialAction.Restrict, operation.OnDelete);
Assert.Equal(ReferentialAction.NoAction, operation.OnDelete);
}),
downOps => Assert.Collection(
downOps,
Expand Down Expand Up @@ -6019,7 +6019,7 @@ public void Change_TPH_to_TPT_with_FKs_and_seed_data()
Assert.Equal("Animal", operation.PrincipalTable);
Assert.Equal(new[] { "MouseId" }, operation.Columns);
Assert.Equal(new[] { "Id" }, operation.PrincipalColumns);
Assert.Equal(ReferentialAction.Restrict, operation.OnDelete);
Assert.Equal(ReferentialAction.NoAction, operation.OnDelete);
},
o =>
{
Expand All @@ -6029,7 +6029,7 @@ public void Change_TPH_to_TPT_with_FKs_and_seed_data()
Assert.Equal("Animal", operation.PrincipalTable);
Assert.Equal(new[] { "PreyId" }, operation.Columns);
Assert.Equal(new[] { "Id" }, operation.PrincipalColumns);
Assert.Equal(ReferentialAction.Restrict, operation.OnDelete);
Assert.Equal(ReferentialAction.NoAction, operation.OnDelete);
}));
}

Expand Down Expand Up @@ -6241,7 +6241,7 @@ public void Change_TPH_to_TPT_with_FKs_and_seed_data_readonly_discriminator()
Assert.Equal("Animal", fk.PrincipalTable);
Assert.Equal(new[] { "PreyId" }, fk.Columns);
Assert.Equal(new[] { "Id" }, fk.PrincipalColumns);
Assert.Equal(ReferentialAction.Restrict, fk.OnDelete);
Assert.Equal(ReferentialAction.NoAction, fk.OnDelete);
});
Assert.Empty(operation.UniqueConstraints);
Expand Down Expand Up @@ -6408,7 +6408,7 @@ public void Change_TPH_to_TPT_with_FKs_and_seed_data_readonly_discriminator()
Assert.Equal("Mice", operation.PrincipalTable);
Assert.Equal(new[] { "MouseId" }, operation.Columns);
Assert.Equal(new[] { "Id" }, operation.PrincipalColumns);
Assert.Equal(ReferentialAction.Restrict, operation.OnDelete);
Assert.Equal(ReferentialAction.NoAction, operation.OnDelete);
},
o =>
{
Expand All @@ -6428,7 +6428,7 @@ public void Change_TPH_to_TPT_with_FKs_and_seed_data_readonly_discriminator()
Assert.Equal("Animal", operation.PrincipalTable);
Assert.Equal(new[] { "PreyId" }, operation.Columns);
Assert.Equal(new[] { "Id" }, operation.PrincipalColumns);
Assert.Equal(ReferentialAction.Restrict, operation.OnDelete);
Assert.Equal(ReferentialAction.NoAction, operation.OnDelete);
}),
downOps => Assert.Collection(
downOps,
Expand Down Expand Up @@ -6697,7 +6697,7 @@ public void Change_TPH_to_TPT_with_FKs_and_seed_data_readonly_discriminator()
Assert.Equal("Animal", operation.PrincipalTable);
Assert.Equal(new[] { "MouseId" }, operation.Columns);
Assert.Equal(new[] { "Id" }, operation.PrincipalColumns);
Assert.Equal(ReferentialAction.Restrict, operation.OnDelete);
Assert.Equal(ReferentialAction.NoAction, operation.OnDelete);
},
o =>
{
Expand All @@ -6707,7 +6707,7 @@ public void Change_TPH_to_TPT_with_FKs_and_seed_data_readonly_discriminator()
Assert.Equal("Animal", operation.PrincipalTable);
Assert.Equal(new[] { "PreyId" }, operation.Columns);
Assert.Equal(new[] { "Id" }, operation.PrincipalColumns);
Assert.Equal(ReferentialAction.Restrict, operation.OnDelete);
Assert.Equal(ReferentialAction.NoAction, operation.OnDelete);
}));
}

Expand Down Expand Up @@ -8244,7 +8244,8 @@ public void SeedData_with_guid_AK_and_multiple_owned_types()
});
target.Entity<ApplicationUser>(
builder => {
builder =>
{
builder.HasAlternateKey(x => x.Guid);
var data = new[]
Expand Down Expand Up @@ -9815,7 +9816,8 @@ public void SeedData_type_with_excluded_owned_collection()
});
},
_ => { },
target => {
target =>
{
target.Entity<Customer>(
c =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ [SSN] nvarchar(11) COLLATE German_PhoneBook_CI_AS NOT NULL,
CONSTRAINT [PK_People] PRIMARY KEY ([CustomId]),
CONSTRAINT [AK_People_SSN] UNIQUE ([SSN]),
CONSTRAINT [CK_People_EmployerId] CHECK ([EmployerId] > 0),
CONSTRAINT [FK_People_Employers_EmployerId] FOREIGN KEY ([EmployerId]) REFERENCES [Employers] ([Id]) ON DELETE NO ACTION
CONSTRAINT [FK_People_Employers_EmployerId] FOREIGN KEY ([EmployerId]) REFERENCES [Employers] ([Id])
);
DECLARE @description AS sql_variant;
SET @description = N'Table comment';
Expand Down Expand Up @@ -1673,15 +1673,15 @@ public override async Task Add_foreign_key()
await base.Add_foreign_key();

AssertSql(
@"ALTER TABLE [Orders] ADD CONSTRAINT [FK_Orders_Customers_CustomerId] FOREIGN KEY ([CustomerId]) REFERENCES [Customers] ([Id]) ON DELETE NO ACTION;");
@"ALTER TABLE [Orders] ADD CONSTRAINT [FK_Orders_Customers_CustomerId] FOREIGN KEY ([CustomerId]) REFERENCES [Customers] ([Id]);");
}

public override async Task Add_foreign_key_with_name()
{
await base.Add_foreign_key_with_name();

AssertSql(
@"ALTER TABLE [Orders] ADD CONSTRAINT [FK_Foo] FOREIGN KEY ([CustomerId]) REFERENCES [Customers] ([Id]) ON DELETE NO ACTION;");
@"ALTER TABLE [Orders] ADD CONSTRAINT [FK_Foo] FOREIGN KEY ([CustomerId]) REFERENCES [Customers] ([Id]);");
}

public override async Task Drop_foreign_key()
Expand Down Expand Up @@ -2830,7 +2830,7 @@ await Test(
c => Assert.Equal("Start", c.Name),
c => Assert.Equal("Name", c.Name),
c => Assert.Equal("Number", c.Name));
Assert.Same(
Assert.Same(
table.Columns.Single(c => c.Name == "Id"),
Assert.Single(table.PrimaryKey!.Columns));
});
Expand Down Expand Up @@ -3715,11 +3715,6 @@ FROM sys.databases
: null;
}

protected override ReferentialAction Normalize(ReferentialAction value)
=> value == ReferentialAction.Restrict
? ReferentialAction.NoAction
: value;

public class MigrationsSqlServerFixture : MigrationsFixtureBase
{
protected override string StoreName { get; } = nameof(MigrationsSqlServerTest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public override async Task Create_table_all_settings()
""SSN"" TEXT COLLATE NOCASE NOT NULL,
CONSTRAINT ""AK_People_SSN"" UNIQUE (""SSN""),
CONSTRAINT ""CK_People_EmployerId"" CHECK (""EmployerId"" > 0),
CONSTRAINT ""FK_People_Employers_EmployerId"" FOREIGN KEY (""EmployerId"") REFERENCES ""Employers"" (""Id"") ON DELETE RESTRICT
CONSTRAINT ""FK_People_Employers_EmployerId"" FOREIGN KEY (""EmployerId"") REFERENCES ""Employers"" (""Id"")
);");
}

Expand Down Expand Up @@ -684,7 +684,7 @@ public override async Task Add_foreign_key()
@"CREATE TABLE ""ef_temp_Orders"" (
""CustomerId"" INTEGER NOT NULL,
""Id"" INTEGER NOT NULL,
CONSTRAINT ""FK_Orders_Customers_CustomerId"" FOREIGN KEY (""CustomerId"") REFERENCES ""Customers"" (""Id"") ON DELETE RESTRICT
CONSTRAINT ""FK_Orders_Customers_CustomerId"" FOREIGN KEY (""CustomerId"") REFERENCES ""Customers"" (""Id"")
);",
@"INSERT INTO ""ef_temp_Orders"" (""CustomerId"", ""Id"")
SELECT ""CustomerId"", ""Id""
Expand All @@ -703,7 +703,7 @@ public override async Task Add_foreign_key_with_name()
@"CREATE TABLE ""ef_temp_Orders"" (
""CustomerId"" INTEGER NOT NULL,
""Id"" INTEGER NOT NULL,
CONSTRAINT ""FK_Foo"" FOREIGN KEY (""CustomerId"") REFERENCES ""Customers"" (""Id"") ON DELETE RESTRICT
CONSTRAINT ""FK_Foo"" FOREIGN KEY (""CustomerId"") REFERENCES ""Customers"" (""Id"")
);",
@"INSERT INTO ""ef_temp_Orders"" (""CustomerId"", ""Id"")
SELECT ""CustomerId"", ""Id""
Expand Down

0 comments on commit ebf6fc9

Please sign in to comment.