Skip to content

Commit 7430b39

Browse files
authored
More triggers to Core (#28700)
Fixes #28673
1 parent 099b2a9 commit 7430b39

File tree

71 files changed

+1474
-1742
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1474
-1742
lines changed

src/EFCore.Design/Extensions/ScaffoldingModelExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,14 +407,14 @@ public static IEnumerable<AttributeCodeFragment> GetDataAnnotations(this ISkipNa
407407
toTableNestedCalls.Add(new MethodCallCodeFragment(nameof(TableBuilder.HasComment), comment));
408408
}
409409

410-
if (entityType.GetTriggers().Any())
410+
if (entityType.GetDeclaredTriggers().Any())
411411
{
412412
toTableHandledByConventions = false;
413413
toTableHandledByDataAnnotations = false;
414414

415-
foreach (var trigger in entityType.GetTriggers())
415+
foreach (var trigger in entityType.GetDeclaredTriggers())
416416
{
417-
toTableNestedCalls.Add(new MethodCallCodeFragment(nameof(TableBuilder.HasTrigger), trigger.Name));
417+
toTableNestedCalls.Add(new MethodCallCodeFragment(nameof(TableBuilder.HasTrigger), trigger.ModelName));
418418
}
419419
}
420420

src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using System.Text;
56
using Microsoft.EntityFrameworkCore.Internal;
7+
using Microsoft.EntityFrameworkCore.Metadata;
68
using Microsoft.EntityFrameworkCore.Metadata.Internal;
79

810
namespace Microsoft.EntityFrameworkCore.Migrations.Design;
@@ -847,28 +849,28 @@ private void GenerateTableMapping(
847849
annotations.TryGetAndRemove(RelationalAnnotationNames.TableName, out IAnnotation tableNameAnnotation);
848850
var table = StoreObjectIdentifier.Create(entityType, StoreObjectType.Table);
849851
var tableName = (string?)tableNameAnnotation?.Value ?? table?.Name;
850-
var explicitName = tableNameAnnotation != null
851-
|| entityType.BaseType == null
852+
var explicitName = tableNameAnnotation != null
853+
|| entityType.BaseType == null
852854
|| entityType.BaseType.GetTableName() != tableName;
853-
855+
854856
annotations.TryGetAndRemove(RelationalAnnotationNames.Schema, out IAnnotation schemaAnnotation);
855857
var schema = (string?)schemaAnnotation?.Value ?? table?.Schema;
856-
858+
857859
annotations.TryGetAndRemove(RelationalAnnotationNames.IsTableExcludedFromMigrations, out IAnnotation isExcludedAnnotation);
858860
var isExcludedFromMigrations = (isExcludedAnnotation?.Value as bool?) == true;
859-
861+
860862
annotations.TryGetAndRemove(RelationalAnnotationNames.Comment, out IAnnotation commentAnnotation);
861863
var comment = (string?)commentAnnotation?.Value;
862-
863-
var hasTriggers = entityType.GetTriggers().Any(t => t.TableName == tableName! && t.TableSchema == schema);
864+
865+
var hasTriggers = entityType.GetDeclaredTriggers().Any(t => t.GetTableName() == tableName! && t.GetTableSchema() == schema);
864866
var hasOverrides = table != null
865867
&& entityType.GetProperties().Select(p => p.FindOverrides(table.Value)).Any(o => o != null);
866868
var requiresTableBuilder = isExcludedFromMigrations
867869
|| comment != null
868870
|| hasTriggers
869871
|| hasOverrides
870872
|| entityType.GetCheckConstraints().Any();
871-
873+
872874
if (!explicitName
873875
&& !requiresTableBuilder)
874876
{
@@ -879,7 +881,7 @@ private void GenerateTableMapping(
879881
.AppendLine()
880882
.Append(entityTypeBuilderName)
881883
.Append(".ToTable(");
882-
884+
883885
if (explicitName)
884886
{
885887
if (tableName == null
@@ -932,7 +934,7 @@ private void GenerateTableMapping(
932934
.AppendLine()
933935
.AppendLine("t.ExcludeFromMigrations();");
934936
}
935-
937+
936938
if (comment != null)
937939
{
938940
stringBuilder
@@ -1106,6 +1108,7 @@ protected virtual void GenerateEntityTypeMappingFragmentAnnotations(
11061108
var annotations = Dependencies.AnnotationCodeGenerator
11071109
.FilterIgnoredAnnotations(fragment.GetAnnotations())
11081110
.ToDictionary(a => a.Name, a => a);
1111+
11091112
if (annotations.Count > 0)
11101113
{
11111114
GenerateAnnotations(tableBuilderName, fragment, stringBuilder, annotations, inChainedCall: false);
@@ -1169,6 +1172,7 @@ protected virtual void GenerateCheckConstraintAnnotations(
11691172
var annotations = Dependencies.AnnotationCodeGenerator
11701173
.FilterIgnoredAnnotations(checkConstraint.GetAnnotations())
11711174
.ToDictionary(a => a.Name, a => a);
1175+
11721176
using (stringBuilder.Indent())
11731177
{
11741178
if (hasNonDefaultName)
@@ -1207,9 +1211,9 @@ protected virtual void GenerateTriggers(
12071211
string? schema,
12081212
IndentedStringBuilder stringBuilder)
12091213
{
1210-
foreach (var trigger in entityType.GetTriggers())
1214+
foreach (var trigger in entityType.GetDeclaredTriggers())
12111215
{
1212-
if (trigger.TableName != table || trigger.TableSchema != schema)
1216+
if (trigger.GetTableName() != table || trigger.GetTableSchema() != schema)
12131217
{
12141218
continue;
12151219
}
@@ -1237,15 +1241,6 @@ protected virtual void GenerateTrigger(
12371241
// Note that GenerateAnnotations below does the corresponding decrement
12381242
stringBuilder.IncrementIndent();
12391243

1240-
if (trigger.Name != trigger.GetDefaultName()!)
1241-
{
1242-
stringBuilder
1243-
.AppendLine()
1244-
.Append(".HasName(")
1245-
.Append(Code.Literal(trigger.Name))
1246-
.Append(")");
1247-
}
1248-
12491244
GenerateTriggerAnnotations(triggerBuilderName, trigger, stringBuilder);
12501245
}
12511246

@@ -1264,6 +1259,18 @@ protected virtual void GenerateTriggerAnnotations(
12641259
.FilterIgnoredAnnotations(trigger.GetAnnotations())
12651260
.ToDictionary(a => a.Name, a => a);
12661261

1262+
if (annotations.TryGetAndRemove(RelationalAnnotationNames.Name, out IAnnotation nameAnnotation))
1263+
{
1264+
stringBuilder
1265+
.AppendLine()
1266+
.Append(".HasName(")
1267+
.Append(Code.Literal((string?)nameAnnotation.Value))
1268+
.Append(")");
1269+
}
1270+
1271+
annotations.Remove(RelationalAnnotationNames.TableName);
1272+
annotations.Remove(RelationalAnnotationNames.Schema);
1273+
12671274
GenerateAnnotations(triggerBuilderName, trigger, stringBuilder, annotations, inChainedCall: true);
12681275
}
12691276

@@ -1337,6 +1344,7 @@ protected virtual void GeneratePropertyOverridesAnnotations(
13371344
var annotations = Dependencies.AnnotationCodeGenerator
13381345
.FilterIgnoredAnnotations(overrides.GetAnnotations())
13391346
.ToDictionary(a => a.Name, a => a);
1347+
13401348
GenerateAnnotations(propertyBuilderName, overrides, stringBuilder, annotations, inChainedCall: true);
13411349
}
13421350

src/EFCore.Design/Scaffolding/Internal/CSharpRuntimeModelCodeGenerator.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,11 @@ private void CreateEntityType(
547547
Create(index, propertyVariables, parameters, nullable);
548548
}
549549

550+
foreach (var trigger in entityType.GetDeclaredTriggers())
551+
{
552+
Create(trigger, parameters);
553+
}
554+
550555
mainBuilder
551556
.Append("return ")
552557
.Append(entityTypeVariable)
@@ -1322,6 +1327,26 @@ private void CreateSkipNavigation(
13221327
.AppendLine("}");
13231328
}
13241329

1330+
private void Create(ITrigger trigger, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
1331+
{
1332+
var triggerVariable = _code.Identifier(trigger.ModelName, parameters.ScopeVariables, capitalize: false);
1333+
1334+
var mainBuilder = parameters.MainBuilder;
1335+
mainBuilder
1336+
.Append("var ").Append(triggerVariable).Append(" = ").Append(parameters.TargetName).AppendLine(".AddTrigger(")
1337+
.IncrementIndent()
1338+
.Append(_code.Literal(trigger.ModelName))
1339+
.AppendLine(");")
1340+
.DecrementIndent();
1341+
1342+
CreateAnnotations(
1343+
trigger,
1344+
_annotationCodeGenerator.Generate,
1345+
parameters with { TargetName = triggerVariable });
1346+
1347+
mainBuilder.AppendLine();
1348+
}
1349+
13251350
private void CreateAnnotations(
13261351
IEntityType entityType,
13271352
IndentedStringBuilder mainBuilder,

src/EFCore.Design/Scaffolding/Internal/RelationalScaffoldingModelFactory.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,9 @@ protected virtual ModelBuilder VisitTables(ModelBuilder modelBuilder, ICollectio
335335
VisitUniqueConstraints(builder, table.UniqueConstraints);
336336
VisitIndexes(builder, table.Indexes);
337337

338-
if (table.FindAnnotation(RelationalAnnotationNames.Triggers) is { Value: HashSet<string> triggers })
338+
foreach (var triggerName in table.Triggers)
339339
{
340-
foreach (var triggerName in triggers)
341-
{
342-
builder.ToTable(table.Name, table.Schema, tb => tb.HasTrigger(triggerName));
343-
}
344-
345-
table.RemoveAnnotation(RelationalAnnotationNames.Triggers);
340+
builder.ToTable(table.Name, table.Schema, tb => tb.HasTrigger(triggerName));
346341
}
347342

348343
builder.Metadata.AddAnnotations(table.GetAnnotations());

src/EFCore.Relational/Design/AnnotationCodeGenerator.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public class AnnotationCodeGenerator : IAnnotationCodeGenerator
2828
private static readonly ISet<string> IgnoredRelationalAnnotations = new HashSet<string>
2929
{
3030
RelationalAnnotationNames.CheckConstraints,
31-
RelationalAnnotationNames.Triggers,
3231
RelationalAnnotationNames.Sequences,
3332
RelationalAnnotationNames.DbFunctions,
3433
RelationalAnnotationNames.DeleteStoredProcedure,
@@ -123,6 +122,18 @@ public virtual void RemoveAnnotationsHandledByConventions(
123122
IDictionary<string, IAnnotation> annotations)
124123
=> RemoveConventionalAnnotationsHelper(foreignKey, annotations, IsHandledByConvention);
125124

125+
/// <inheritdoc />
126+
public virtual void RemoveAnnotationsHandledByConventions(
127+
INavigation navigation,
128+
IDictionary<string, IAnnotation> annotations)
129+
=> RemoveConventionalAnnotationsHelper(navigation, annotations, IsHandledByConvention);
130+
131+
/// <inheritdoc />
132+
public virtual void RemoveAnnotationsHandledByConventions(
133+
ISkipNavigation navigation,
134+
IDictionary<string, IAnnotation> annotations)
135+
=> RemoveConventionalAnnotationsHelper(navigation, annotations, IsHandledByConvention);
136+
126137
/// <inheritdoc />
127138
public virtual void RemoveAnnotationsHandledByConventions(IIndex index, IDictionary<string, IAnnotation> annotations)
128139
=> RemoveConventionalAnnotationsHelper(index, annotations, IsHandledByConvention);
@@ -543,6 +554,32 @@ protected virtual bool IsHandledByConvention(IProperty property, IAnnotation ann
543554
protected virtual bool IsHandledByConvention(IForeignKey foreignKey, IAnnotation annotation)
544555
=> false;
545556

557+
/// <summary>
558+
/// Checks if the given <paramref name="annotation" /> is handled by convention when
559+
/// applied to the given <paramref name="navigation" />.
560+
/// </summary>
561+
/// <remarks>
562+
/// The default implementation always returns <see langword="false" />.
563+
/// </remarks>
564+
/// <param name="navigation">The <see cref="INavigation" />.</param>
565+
/// <param name="annotation">The <see cref="IAnnotation" />.</param>
566+
/// <returns><see langword="false" />.</returns>
567+
protected virtual bool IsHandledByConvention(INavigation navigation, IAnnotation annotation)
568+
=> false;
569+
570+
/// <summary>
571+
/// Checks if the given <paramref name="annotation" /> is handled by convention when
572+
/// applied to the given <paramref name="navigation" />.
573+
/// </summary>
574+
/// <remarks>
575+
/// The default implementation always returns <see langword="false" />.
576+
/// </remarks>
577+
/// <param name="navigation">The <see cref="ISkipNavigation" />.</param>
578+
/// <param name="annotation">The <see cref="IAnnotation" />.</param>
579+
/// <returns><see langword="false" />.</returns>
580+
protected virtual bool IsHandledByConvention(ISkipNavigation navigation, IAnnotation annotation)
581+
=> false;
582+
546583
/// <summary>
547584
/// Checks if the given <paramref name="annotation" /> is handled by convention when
548585
/// applied to the given <paramref name="index" />.

src/EFCore.Relational/Design/IAnnotationCodeGenerator.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,26 @@ void RemoveAnnotationsHandledByConventions(IForeignKey foreignKey, IDictionary<s
8888
{
8989
}
9090

91+
/// <summary>
92+
/// Removes annotation whose configuration is already applied by convention, and do not need to be
93+
/// specified explicitly.
94+
/// </summary>
95+
/// <param name="navigation">The navigation to which the annotations are applied.</param>
96+
/// <param name="annotations">The set of annotations from which to remove the conventional ones.</param>
97+
void RemoveAnnotationsHandledByConventions(INavigation navigation, IDictionary<string, IAnnotation> annotations)
98+
{
99+
}
100+
101+
/// <summary>
102+
/// Removes annotation whose configuration is already applied by convention, and do not need to be
103+
/// specified explicitly.
104+
/// </summary>
105+
/// <param name="navigation">The navigation to which the annotations are applied.</param>
106+
/// <param name="annotations">The set of annotations from which to remove the conventional ones.</param>
107+
void RemoveAnnotationsHandledByConventions(ISkipNavigation navigation, IDictionary<string, IAnnotation> annotations)
108+
{
109+
}
110+
91111
/// <summary>
92112
/// Removes annotation whose configuration is already applied by convention, and do not need to be
93113
/// specified explicitly.
@@ -139,8 +159,8 @@ void RemoveAnnotationsHandledByConventions(ISequence sequence, IDictionary<strin
139159
}
140160

141161
/// <summary>
142-
/// For the given annotations which have corresponding fluent API calls, returns those fluent API calls
143-
/// and removes the annotations.
162+
/// Removes annotation whose configuration is already applied by convention, and do not need to be
163+
/// specified explicitly.
144164
/// </summary>
145165
/// <param name="annotatable">The annotatable to which the annotations are applied.</param>
146166
/// <param name="annotations">The set of annotations from which to generate fluent API calls.</param>

0 commit comments

Comments
 (0)