-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a custom convention which adds a blank trigger to all tables
Fixes #27531
- Loading branch information
1 parent
4dd5074
commit 7d5f0ac
Showing
6 changed files
with
154 additions
and
8 deletions.
There are no files selected for viewing
2 changes: 0 additions & 2 deletions
2
src/EFCore.Relational/Extensions/RelationalTriggerExtensions.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
62 changes: 62 additions & 0 deletions
62
src/EFCore.Relational/Metadata/Conventions/BlankTriggerAddingConvention.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,62 @@ | ||
// 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.Metadata.Conventions; | ||
|
||
/// <summary> | ||
/// A convention that makes sure there is a trigger on all entity types. | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-conventions">Model building conventions</see> for more information and examples. | ||
/// </remarks> | ||
public class BlankTriggerAddingConvention : IModelFinalizingConvention | ||
{ | ||
/// <summary> | ||
/// Creates a new instance of <see cref="BlankTriggerAddingConvention" />. | ||
/// </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 BlankTriggerAddingConvention( | ||
ProviderConventionSetBuilderDependencies dependencies, | ||
RelationalConventionSetBuilderDependencies relationalDependencies) | ||
{ | ||
Dependencies = dependencies; | ||
RelationalDependencies = relationalDependencies; | ||
} | ||
|
||
/// <summary> | ||
/// Dependencies for this convention. | ||
/// </summary> | ||
protected virtual ProviderConventionSetBuilderDependencies Dependencies { get; } | ||
|
||
/// <summary> | ||
/// Relational provider-specific dependencies for this service. | ||
/// </summary> | ||
protected virtual RelationalConventionSetBuilderDependencies RelationalDependencies { get; } | ||
|
||
/// <summary> | ||
/// Called when a model is being finalized. | ||
/// </summary> | ||
/// <param name="modelBuilder">The builder for the model.</param> | ||
/// <param name="context">Additional information associated with convention execution.</param> | ||
public virtual void ProcessModelFinalizing(IConventionModelBuilder modelBuilder, IConventionContext<IConventionModelBuilder> context) | ||
{ | ||
foreach (var entityType in modelBuilder.Metadata.GetEntityTypes()) | ||
{ | ||
var table = StoreObjectIdentifier.Create(entityType, StoreObjectType.Table); | ||
if (table != null | ||
&& entityType.GetDeclaredTriggers().All(t => t.GetName(table.Value) == null)) | ||
{ | ||
entityType.Builder.HasTrigger(table.Value.Name + "_Trigger"); | ||
} | ||
|
||
foreach (var fragment in entityType.GetMappingFragments(StoreObjectType.Table)) | ||
{ | ||
if (entityType.GetDeclaredTriggers().All(t => t.GetName(fragment.StoreObject) == null)) | ||
{ | ||
entityType.Builder.HasTrigger(fragment.StoreObject.Name + "_Trigger"); | ||
} | ||
} | ||
} | ||
} | ||
} |
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,26 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
// ReSharper disable CheckNamespace | ||
|
||
namespace Microsoft.EntityFrameworkCore.Internal; | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public static class DbContextExtensions | ||
{ | ||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public static void ConfigureConventions( | ||
this DbContext context, | ||
ModelConfigurationBuilder configurationBuilder) | ||
=> context.ConfigureConventions(configurationBuilder); | ||
} |
55 changes: 55 additions & 0 deletions
55
test/EFCore.Relational.Tests/Metadata/Conventions/BlankTriggerAddingConventionTest.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,55 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#nullable enable | ||
|
||
// ReSharper disable InconsistentNaming | ||
namespace Microsoft.EntityFrameworkCore.Metadata.Conventions; | ||
|
||
public class BlankTriggerAddingConventionTest | ||
{ | ||
[ConditionalFact] | ||
public virtual void Adds_triggers_with_table_splitting() | ||
{ | ||
var modelBuilder = CreateModelBuilder(); | ||
|
||
modelBuilder.Entity<Order>().SplitToTable("OrderDetails", s => s.Property(o => o.CustomerId)); | ||
|
||
var model = modelBuilder.FinalizeModel(); | ||
|
||
var entity = model.FindEntityType(typeof(Order))!; | ||
|
||
Assert.Equal(new[] { "OrderDetails_Trigger", "Order_Trigger" }, entity.GetDeclaredTriggers().Select(t => t.ModelName)); | ||
} | ||
|
||
[ConditionalFact] | ||
public virtual void Does_not_add_triggers_without_tables() | ||
{ | ||
var modelBuilder = CreateModelBuilder(); | ||
|
||
modelBuilder.Entity<Order>().ToView("Orders"); | ||
modelBuilder.Entity<Order>().SplitToView("OrderDetails", s => s.Property(o => o.CustomerId)); | ||
|
||
var model = modelBuilder.FinalizeModel(); | ||
|
||
var entity = model.FindEntityType(typeof(Order))!; | ||
|
||
Assert.Empty(entity.GetDeclaredTriggers()); | ||
} | ||
|
||
protected class Order | ||
{ | ||
public int OrderId { get; set; } | ||
|
||
public int? CustomerId { get; set; } | ||
public Guid AnotherCustomerId { get; set; } | ||
} | ||
|
||
protected virtual ModelBuilder CreateModelBuilder() | ||
=> FakeRelationalTestHelpers.Instance.CreateConventionBuilder( | ||
configureModel: | ||
b => b.Conventions.Add( | ||
p => new BlankTriggerAddingConvention( | ||
p.GetRequiredService<ProviderConventionSetBuilderDependencies>(), | ||
p.GetRequiredService<RelationalConventionSetBuilderDependencies>()))); | ||
} |
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