2025-11-12 14:53:23 +00:00
using System.Diagnostics.CodeAnalysis ;
using System.Reflection ;
2025-09-27 18:39:18 +00:00
using Microsoft.EntityFrameworkCore ;
using Microsoft.EntityFrameworkCore.Diagnostics ;
using Microsoft.EntityFrameworkCore.Infrastructure ;
using Microsoft.EntityFrameworkCore.Migrations ;
using Microsoft.EntityFrameworkCore.Migrations.Internal ;
namespace Elsa.Persistence.EFCore ;
/// <summary>
/// Class That enable Schema change for Migration
/// </summary>
2025-11-12 14:53:23 +00:00
[SuppressMessage("Usage", "EF1001:Internal EF Core API usage.", Justification = "Required to customize migration assembly behavior for schema-aware migrations.")]
2025-09-27 18:39:18 +00:00
public class DbSchemaAwareMigrationAssembly (
ICurrentDbContext currentContext ,
IDbContextOptions options ,
IMigrationsIdGenerator idGenerator ,
IDiagnosticsLogger < DbLoggerCategory . Migrations > logger )
: MigrationsAssembly ( currentContext , options , idGenerator , logger )
{
private readonly DbContext _context = currentContext . Context ;
public override Migration CreateMigration ( TypeInfo migrationClass , string activeProvider )
{
if ( activeProvider = = null )
throw new ArgumentNullException ( nameof ( activeProvider ) ) ;
var hasCtorWithSchema = migrationClass . GetConstructor ( [ typeof ( IElsaDbContextSchema ) ] ) ! = null ;
if ( hasCtorWithSchema & & _context is IElsaDbContextSchema schema )
{
var instance = ( Migration ) Activator . CreateInstance ( migrationClass . AsType ( ) , schema ) ! ;
instance . ActiveProvider = activeProvider ;
return instance ;
}
return base . CreateMigration ( migrationClass , activeProvider ) ;
}
}