Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,14 @@ protected virtual void Generate(AddForeignKeyOperation operation, IndentedString
.Append(Code.Literal(operation.OnDelete));
}

if (operation.Deferrability != ConstraintDeferrability.NotDeferrable)
{
builder
.AppendLine(",")
.Append("deferrability: ")
.Append(Code.Literal(operation.Deferrability));
}

builder.Append(")");

Annotations(operation.GetAnnotations(), builder);
Expand Down Expand Up @@ -1308,6 +1316,14 @@ protected virtual void Generate(CreateTableOperation operation, IndentedStringBu
.Append(Code.Literal(foreignKey.OnDelete));
}

if (foreignKey.Deferrability != ConstraintDeferrability.NotDeferrable)
{
builder
.AppendLine(",")
.Append("deferrability: ")
.Append(Code.Literal(foreignKey.Deferrability));
}

builder.Append(")");

Annotations(foreignKey.GetAnnotations(), builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ protected virtual IEnumerable<string> GetNamespaces(IEnumerable<MigrationOperati
.Concat(operations.OfType<UpdateDataOperation>().SelectMany(o => new[] { o.KeyValues, o.Values }))
.Concat(operations.OfType<DeleteDataOperation>().Select(o => o.KeyValues))
.SelectMany(GetDataNamespaces))
.Concat(
operations.OfType<AddForeignKeyOperation>()
.Concat(operations.OfType<CreateTableOperation>().SelectMany(o => o.ForeignKeys))
.Any(o => o.Deferrability != ConstraintDeferrability.NotDeferrable)
? [typeof(ConstraintDeferrability).Namespace!]
: [])
.Concat(GetAnnotationNamespaces(GetAnnotatables(operations)));

private static IEnumerable<string> GetColumnNamespaces(ColumnOperation columnOperation)
Expand Down
38 changes: 38 additions & 0 deletions src/EFCore.Relational/ConstraintDeferrability.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore;

/// <summary>
/// Indicates whether checking of a database constraint can be deferred until the end of the transaction,
/// as defined by the SQL standard (<c>[NOT] DEFERRABLE INITIALLY {IMMEDIATE | DEFERRED}</c>).
/// </summary>
/// <remarks>
/// <para>
/// Not all databases support deferrable constraints; for example, PostgreSQL, Oracle and SQLite (foreign keys only)
/// support them, while SQL Server and MySQL do not.
/// </para>
/// <para>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and
/// examples.
/// </para>
/// </remarks>
public enum ConstraintDeferrability
{
/// <summary>
/// The constraint is checked immediately after every statement, and checking cannot be deferred (<c>NOT DEFERRABLE</c>).
/// This is the default.
/// </summary>
NotDeferrable = 0,

/// <summary>
/// The constraint is checked immediately after every statement by default, but checking can be deferred to the
/// end of the transaction (e.g. with <c>SET CONSTRAINTS ... DEFERRED</c>) (<c>DEFERRABLE INITIALLY IMMEDIATE</c>).
/// </summary>
DeferrableInitiallyImmediate = 1,

/// <summary>
/// The constraint is checked at the end of the transaction (<c>DEFERRABLE INITIALLY DEFERRED</c>).
/// </summary>
DeferrableInitiallyDeferred = 2
}
6 changes: 6 additions & 0 deletions src/EFCore.Relational/Design/AnnotationCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,12 @@ public virtual IReadOnlyList<MethodCallCodeFragment> GenerateFluentApiCalls(
nameof(RelationalForeignKeyBuilderExtensions.ExcludeForeignKeyFromMigrations),
methodCallCodeFragments);

GenerateSimpleFluentApiCall(
annotations,
RelationalAnnotationNames.Deferrability,
nameof(RelationalForeignKeyBuilderExtensions.HasDeferrability),
methodCallCodeFragments);

methodCallCodeFragments.AddRange(GenerateFluentApiCallsHelper(foreignKey, annotations, GenerateFluentApi));

return methodCallCodeFragments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2467,6 +2467,7 @@ public override void Generate(IForeignKey foreignKey, CSharpRuntimeAnnotationCod
else
{
parameters.Annotations.Remove(RelationalAnnotationNames.IsForeignKeyExcludedFromMigrations);
parameters.Annotations.Remove(RelationalAnnotationNames.Deferrability);
}

base.Generate(foreignKey, parameters);
Expand Down
Loading
Loading