Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scaffolding DatabaseTrigger class #28801

Merged
merged 1 commit into from
Aug 21, 2022
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 @@ -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