Skip to content

Commit

Permalink
Add scaffolding DatabaseTrigger class
Browse files Browse the repository at this point in the history
Part of #27588
  • Loading branch information
roji committed Aug 21, 2022
1 parent 3ad27d0 commit 939ee43
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,11 @@ protected virtual ModelBuilder VisitTables(ModelBuilder modelBuilder, ICollectio
VisitUniqueConstraints(builder, table.UniqueConstraints);
VisitIndexes(builder, table.Indexes);

foreach (var triggerName in table.Triggers)
foreach (var trigger in table.Triggers)
{
builder.ToTable(table.Name, table.Schema, tb => tb.HasTrigger(triggerName));
builder.ToTable(table.Name, table.Schema, tb => tb
.HasTrigger(trigger.Name)
.Metadata.AddAnnotations(trigger.GetAnnotations()));
}

builder.Metadata.AddAnnotations(table.GetAnnotations());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class DatabaseTable : Annotatable
/// <summary>
/// The list of triggers defined on the table.
/// </summary>
public virtual HashSet<string> Triggers { get; } = new HashSet<string>();
public virtual IList<DatabaseTrigger> Triggers { get; } = new List<DatabaseTrigger>();

/// <inheritdoc />
public override string ToString()
Expand Down
19 changes: 19 additions & 0 deletions src/EFCore.Relational/Scaffolding/Metadata/DatabaseTrigger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.Scaffolding.Metadata;

/// <summary>
/// A simple model for a database trigger used when reverse engineering an existing database.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-scaffolding">Reverse engineering (scaffolding) an existing database</see>, and
/// <see href="https://aka.ms/efcore-docs-design-time-services">EF Core design-time services</see> for more information and examples.
/// </remarks>
public class DatabaseTrigger : Annotatable
{
/// <summary>
/// The trigger name.
/// </summary>
public virtual string Name { get; set; } = null!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,7 @@ FROM [sys].[triggers] AS [tr]

// We don't actually scaffold anything beyond the fact that there's a trigger with a given name.
// This is to modify the SaveChanges logic to not use OUTPUT without INTO, which is incompatible with triggers.
table.Triggers.Add(triggerName);
table.Triggers.Add(new DatabaseTrigger { Name = triggerName });
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -914,9 +914,9 @@ AFTER INSERT AS
var table = dbModel.Tables.Single();
var triggers = table.Triggers;
Assert.Collection(triggers.OrderBy(t => t),
t => Assert.Equal("Trigger1", t),
t => Assert.Equal("Trigger2", t));
Assert.Collection(triggers.OrderBy(t => t.Name),
t => Assert.Equal("Trigger1", t.Name),
t => Assert.Equal("Trigger2", t.Name));
},
"DROP TABLE SomeTable;");
Expand Down

0 comments on commit 939ee43

Please sign in to comment.