Skip to content

Commit f2bb6b7

Browse files
committed
SQL Server: report deferrable foreign keys as a model validation error
SqlServerModelValidator throws DeferrableForeignKeysNotSupported when a foreign key is configured as deferrable, following SQLite's handling of unsupported features. SqlServerMigrationsSqlGenerator still throws NotSupportedException for migration operations without a model.
1 parent 3550e4c commit f2bb6b7

4 files changed

Lines changed: 80 additions & 2 deletions

File tree

‎src/EFCore.SqlServer/Infrastructure/Internal/SqlServerModelValidator.cs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,32 @@ protected override void ValidateProperty(
5757
ValidateDecimalColumn(property, logger);
5858
}
5959

60+
/// <inheritdoc />
61+
protected override void ValidateForeignKey(
62+
IForeignKey foreignKey,
63+
IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)
64+
{
65+
base.ValidateForeignKey(foreignKey, logger);
66+
67+
ValidateForeignKeyDeferrability(foreignKey);
68+
}
69+
70+
/// <summary>
71+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
72+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
73+
/// any release. You should only use it directly in your code with extreme caution and knowing that
74+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
75+
/// </summary>
76+
protected virtual void ValidateForeignKeyDeferrability(IForeignKey foreignKey)
77+
{
78+
if (foreignKey.GetDeferrability() != ConstraintDeferrability.NotDeferrable)
79+
{
80+
throw new InvalidOperationException(
81+
SqlServerStrings.DeferrableForeignKeysNotSupported(
82+
foreignKey.Properties.Format(), foreignKey.DeclaringEntityType.DisplayName()));
83+
}
84+
}
85+
6086
/// <summary>
6187
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
6288
/// the same compatibility standards as public APIs. It may be changed or removed without notice in

‎src/EFCore.SqlServer/Properties/SqlServerStrings.Designer.cs‎

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/EFCore.SqlServer/Properties/SqlServerStrings.resx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@
136136
<value>'DateTimeOffset.Offset' cannot be translated on its own; use 'DateTimeOffset.Offset.TotalMinutes' to get the offset in minutes.</value>
137137
</data>
138138
<data name="DeferrableConstraintsNotSupported" xml:space="preserve">
139-
<value>The foreign key constraint '{constraint}' on table '{table}' is configured as deferrable, but SQL Server does not support deferrable constraints.</value>
139+
<value>SQL Server does not support deferrable constraints, but the foreign key constraint '{constraint}' on table '{table}' is configured as deferrable.</value>
140+
</data>
141+
<data name="DeferrableForeignKeysNotSupported" xml:space="preserve">
142+
<value>SQL Server does not support deferrable constraints, but the foreign key {foreignKeyProperties} on entity type '{entityType}' has been configured as deferrable.</value>
140143
</data>
141144
<data name="DuplicateColumnIdentityIncrementMismatch" xml:space="preserve">
142145
<value>'{entityType1}.{property1}' and '{entityType2}.{property2}' are both mapped to column '{columnName}' in '{table}', but are configured with different identity increment values.</value>

‎test/EFCore.SqlServer.Tests/Infrastructure/SqlServerModelValidatorTest.cs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,47 @@ public override void Detects_store_generated_PK_in_TPC()
9797
Assert.Equal(SqlServerValueGenerationStrategy.Sequence, keyProperty.GetValueGenerationStrategy());
9898
}
9999

100+
[Theory]
101+
[InlineData(ConstraintDeferrability.DeferrableInitiallyImmediate)]
102+
[InlineData(ConstraintDeferrability.DeferrableInitiallyDeferred)]
103+
public virtual void Throws_for_deferrable_foreign_key(ConstraintDeferrability deferrability)
104+
{
105+
var modelBuilder = CreateConventionModelBuilder();
106+
modelBuilder.Entity<Animal>();
107+
modelBuilder.Entity<Cat>().HasOne<Person>().WithMany().HasForeignKey(c => c.Name).HasPrincipalKey(p => p.Name)
108+
.HasDeferrability(deferrability);
109+
110+
VerifyError(
111+
SqlServerStrings.DeferrableForeignKeysNotSupported("{'" + nameof(Cat.Name) + "'}", nameof(Cat)),
112+
modelBuilder);
113+
}
114+
115+
[Fact]
116+
public virtual void Passes_for_foreign_key_not_deferrable()
117+
{
118+
var modelBuilder = CreateConventionModelBuilder();
119+
modelBuilder.Entity<Animal>();
120+
modelBuilder.Entity<Cat>().HasOne<Person>().WithMany().HasForeignKey(c => c.Name).HasPrincipalKey(p => p.Name)
121+
.HasDeferrability(ConstraintDeferrability.NotDeferrable);
122+
modelBuilder.Entity<Dog>().HasOne<Person>().WithMany().HasForeignKey(d => d.Name).HasPrincipalKey(p => p.Name);
123+
124+
Validate(modelBuilder);
125+
}
126+
127+
public override void Detects_duplicate_foreignKey_names_within_hierarchy_with_different_deferrability()
128+
{
129+
var modelBuilder = CreateConventionModelBuilder();
130+
modelBuilder.Entity<Animal>();
131+
modelBuilder.Entity<Cat>().HasOne<Person>().WithMany().HasForeignKey(c => c.Name).HasPrincipalKey(p => p.Name)
132+
.HasConstraintName("FK_Animal_Person_Name").HasDeferrability(ConstraintDeferrability.DeferrableInitiallyDeferred);
133+
modelBuilder.Entity<Dog>().HasOne<Person>().WithMany().HasForeignKey(d => d.Name).HasPrincipalKey(p => p.Name)
134+
.HasConstraintName("FK_Animal_Person_Name");
135+
136+
VerifyError(
137+
SqlServerStrings.DeferrableForeignKeysNotSupported("{'" + nameof(Cat.Name) + "'}", nameof(Cat)),
138+
modelBuilder);
139+
}
140+
100141
[Fact]
101142
public virtual void Throws_for_identity_on_bad_type()
102143
{

0 commit comments

Comments
 (0)