diff --git a/src/Laraue.EfCoreTriggers.Common/SqlGeneration/ISqlGenerator.cs b/src/Laraue.EfCoreTriggers.Common/SqlGeneration/ISqlGenerator.cs
index ac2463b..c5219b3 100644
--- a/src/Laraue.EfCoreTriggers.Common/SqlGeneration/ISqlGenerator.cs
+++ b/src/Laraue.EfCoreTriggers.Common/SqlGeneration/ISqlGenerator.cs
@@ -56,6 +56,13 @@ public interface ISqlGenerator
///
///
string GetTableSql(Type entity);
+
+ ///
+ /// Get schema prefix SQL, e.g. "dbo".
+ ///
+ ///
+ ///
+ string? GetSchemaPrefixSql(Type entity);
///
/// Get the function name with the entities schema.
diff --git a/src/Laraue.EfCoreTriggers.Common/SqlGeneration/SqlGenerator.cs b/src/Laraue.EfCoreTriggers.Common/SqlGeneration/SqlGenerator.cs
index 0445d8a..563b87b 100644
--- a/src/Laraue.EfCoreTriggers.Common/SqlGeneration/SqlGenerator.cs
+++ b/src/Laraue.EfCoreTriggers.Common/SqlGeneration/SqlGenerator.cs
@@ -96,12 +96,19 @@ public string GetColumnSql(Type type, MemberInfo memberInfo, ArgumentType argume
///
public string GetTableSql(Type entity)
{
- var schemaName = _adapter.GetTableSchemaName(entity);
+ var schemaPrefix = GetSchemaPrefixSql(entity);
var tableSql = WrapWithDelimiters(_adapter.GetTableName(entity));
- return string.IsNullOrWhiteSpace(schemaName)
- ? tableSql
- : $"{WrapWithDelimiters(schemaName)}.{tableSql}";
+ return $"{schemaPrefix}{tableSql}";
+ }
+
+ ///
+ public string? GetSchemaPrefixSql(Type entity)
+ {
+ var schemaName = _adapter.GetTableSchemaName(entity);
+ return string.IsNullOrEmpty(schemaName)
+ ? null
+ : $"{WrapWithDelimiters(schemaName)}.";
}
///
@@ -115,7 +122,7 @@ public string GetFunctionNameSql(Type entity, string name)
{
return GetFunctionNameSql(_adapter.GetTableSchemaName(entity), name);
}
-
+
private string GetFunctionNameSql(string? schemaName, string triggerName)
{
var functionName = WrapWithDelimiters(triggerName);
diff --git a/src/Laraue.EfCoreTriggers.SqlServer/SqlServerTriggerVisitor.cs b/src/Laraue.EfCoreTriggers.SqlServer/SqlServerTriggerVisitor.cs
index 2fa2c09..c3ebd9c 100644
--- a/src/Laraue.EfCoreTriggers.SqlServer/SqlServerTriggerVisitor.cs
+++ b/src/Laraue.EfCoreTriggers.SqlServer/SqlServerTriggerVisitor.cs
@@ -196,7 +196,9 @@ private static string GetDeclareCursorSql(string cursorName)
public override string GenerateDeleteTriggerSql(string triggerName, IEntityType entityType)
{
- return $"DROP TRIGGER {triggerName};";
+ var tableSchemaPrefix = _sqlGenerator.GetSchemaPrefixSql(entityType.ClrType);
+
+ return $"DROP TRIGGER {tableSchemaPrefix}{triggerName};";
}
private string GetSelectFromCursorSql(Type triggerEntityType, ArgumentType argumentType, MemberInfo[] members)
diff --git a/tests/Laraue.EfCoreTriggers.Tests/Infrastructure/DynamicDbContext.cs b/tests/Laraue.EfCoreTriggers.Tests/Infrastructure/DynamicDbContext.cs
index 02161f4..0defda4 100644
--- a/tests/Laraue.EfCoreTriggers.Tests/Infrastructure/DynamicDbContext.cs
+++ b/tests/Laraue.EfCoreTriggers.Tests/Infrastructure/DynamicDbContext.cs
@@ -1,6 +1,10 @@
using System;
+using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Metadata;
+using Microsoft.EntityFrameworkCore.Migrations;
namespace Laraue.EfCoreTriggers.Tests.Infrastructure
{
@@ -30,14 +34,45 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
public override async ValueTask DisposeAsync()
{
+ foreach (var command in GetMigrationDownCommands())
+ {
+ await Database.ExecuteSqlRawAsync(command.CommandText);
+ }
+
await Database.EnsureDeletedAsync();
await base.DisposeAsync();
}
public override void Dispose()
{
+ foreach (var command in GetMigrationDownCommands())
+ {
+ Database.ExecuteSqlRaw(command.CommandText);
+ }
+
Database.EnsureDeleted();
base.Dispose();
}
+
+ private IReadOnlyList GetMigrationDownCommands()
+ {
+ var relationalModel = GetRelationalModel();
+
+ var migrationsModelDiffer = Database.GetService();
+ var downMigrationDifferences = migrationsModelDiffer
+ .GetDifferences(relationalModel, null);
+
+ var migrationsSqlGenerator = Database.GetService();
+ return migrationsSqlGenerator.Generate(downMigrationDifferences);
+ }
+
+ private IRelationalModel GetRelationalModel()
+ {
+#if NET5_0
+ return Model.GetRelationalModel();
+#else
+ return Database.GetService().Model.GetRelationalModel();
+#endif
+ }
}
}
\ No newline at end of file
diff --git a/tests/Laraue.EfCoreTriggers.Tests/Infrastructure/DynamicDbContextFactory.cs b/tests/Laraue.EfCoreTriggers.Tests/Infrastructure/DynamicDbContextFactory.cs
index a7b41a6..1eadcc4 100644
--- a/tests/Laraue.EfCoreTriggers.Tests/Infrastructure/DynamicDbContextFactory.cs
+++ b/tests/Laraue.EfCoreTriggers.Tests/Infrastructure/DynamicDbContextFactory.cs
@@ -11,6 +11,7 @@ public static DynamicDbContext GetDbContext(IContextOptionsFactory