forked from dotnet/efcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow opting out of RETURNING/OUTPUT clauses in SaveChanges
Fixes dotnet#29916
- Loading branch information
Showing
35 changed files
with
1,081 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
src/EFCore.SqlServer/Metadata/Conventions/SqlServerOutputClauseConvention.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.EntityFrameworkCore.Metadata.Conventions; | ||
|
||
/// <summary> | ||
/// A convention that configures tables with triggers to not use the OUTPUT clause when saving changes. | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-conventions">Model building conventions</see>, and | ||
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and SQL Azure databases with EF Core</see> | ||
/// for more information and examples. | ||
/// </remarks> | ||
public class SqlServerOutputClauseConvention | ||
: ITriggerAddedConvention, ITriggerRemovedConvention, IEntityTypeBaseTypeChangedConvention | ||
{ | ||
/// <summary> | ||
/// Creates a new instance of <see cref="SqlServerDbFunctionConvention" />. | ||
/// </summary> | ||
/// <param name="dependencies">Parameter object containing dependencies for this convention.</param> | ||
/// <param name="relationalDependencies"> Parameter object containing relational dependencies for this convention.</param> | ||
public SqlServerOutputClauseConvention( | ||
ProviderConventionSetBuilderDependencies dependencies, | ||
RelationalConventionSetBuilderDependencies relationalDependencies) | ||
{ | ||
Dependencies = dependencies; | ||
RelationalDependencies = relationalDependencies; | ||
} | ||
|
||
/// <summary> | ||
/// Dependencies for this service. | ||
/// </summary> | ||
protected virtual ProviderConventionSetBuilderDependencies Dependencies { get; } | ||
|
||
/// <summary> | ||
/// Relational provider-specific dependencies for this service. | ||
/// </summary> | ||
protected virtual RelationalConventionSetBuilderDependencies RelationalDependencies { get; } | ||
|
||
/// <inheritdoc /> | ||
public virtual void ProcessTriggerAdded(IConventionTriggerBuilder triggerBuilder, IConventionContext<IConventionTriggerBuilder> context) | ||
{ | ||
var entityType = triggerBuilder.Metadata.EntityType; | ||
|
||
if (entityType.GetMappingStrategy() == RelationalAnnotationNames.TphMappingStrategy) | ||
{ | ||
entityType = entityType.GetRootType(); | ||
} | ||
|
||
entityType.UseSqlOutputClause(false); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public virtual void ProcessTriggerRemoved( | ||
IConventionEntityTypeBuilder entityTypeBuilder, | ||
IConventionTrigger trigger, | ||
IConventionContext<IConventionTrigger> context) | ||
{ | ||
var entityType = entityTypeBuilder.Metadata; | ||
|
||
if (entityType.GetMappingStrategy() == RelationalAnnotationNames.TphMappingStrategy | ||
&& entityType.GetRootType() is { } rootEntityType | ||
&& !HasAnyTriggers(rootEntityType)) | ||
{ | ||
rootEntityType.UseSqlOutputClause(null); | ||
} | ||
else if (!HasAnyTriggers(entityType)) | ||
{ | ||
entityType.UseSqlOutputClause(null); | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void ProcessEntityTypeBaseTypeChanged( | ||
IConventionEntityTypeBuilder entityTypeBuilder, | ||
IConventionEntityType? newBaseType, | ||
IConventionEntityType? oldBaseType, | ||
IConventionContext<IConventionEntityType> context) | ||
{ | ||
var entityType = entityTypeBuilder.Metadata; | ||
|
||
if (entityTypeBuilder.Metadata.GetMappingStrategy() == RelationalAnnotationNames.TphMappingStrategy) | ||
{ | ||
// Update the old TPH root, removing or adding the setting if triggers are found anywhere in the hierarchy | ||
if (oldBaseType?.GetRootType() is { } oldRootEntityType | ||
&& !oldRootEntityType.GetIsSqlOutputClauseUsed() | ||
&& !HasAnyTriggers(oldRootEntityType)) | ||
{ | ||
oldRootEntityType.UseSqlOutputClause(null); | ||
} | ||
|
||
if (newBaseType?.GetRootType() is { } newRootEntityType | ||
&& newRootEntityType.GetIsSqlOutputClauseUsed() | ||
&& HasAnyTriggers(entityType)) | ||
{ | ||
newRootEntityType.UseSqlOutputClause(false); | ||
} | ||
|
||
// If the entity type is being added to a TPH hierarchy, remove the annotation from it, since we've added it to the root | ||
if (newBaseType is not null) | ||
{ | ||
entityType.UseSqlOutputClause(null); | ||
} | ||
|
||
return; | ||
} | ||
|
||
// If the entity was removed from a TPH hierarchy, we may need to add the setting to the entity itself | ||
if (newBaseType is null) | ||
{ | ||
entityType.UseSqlOutputClause(HasAnyTriggers(entityType) ? false : null); | ||
} | ||
} | ||
|
||
private bool HasAnyTriggers(IConventionEntityType entityType) | ||
{ | ||
if (entityType.GetDeclaredTriggers().Any()) | ||
{ | ||
return true; | ||
} | ||
|
||
if (entityType.GetMappingStrategy() == RelationalAnnotationNames.TphMappingStrategy | ||
&& entityType.GetDerivedTypes().Any(t => t.GetDeclaredTriggers().Any())) | ||
{ | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.