我正在研究一个可重复使用的小软件包för.net核心应用程序,该应用程序将有助于在应用程序启动时自动迁移。
它基本上将在每个DbContext
上运行Database.Migrate()
。
,但这里的问题是,我只想在自动移民"标记"的dbcontexts上运行它。我认为我可以扩展AddDbContext
,并以某种方式告诉IServiceCollection
以跟踪特定的DbContext
。这样的东西:
public static IServiceCollection AddDbContextWithMigration<TContext>(this IServiceCollection serviceCollection, Action<DbContextOptionsBuilder> optionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Scoped, ServiceLifetime optionsLifetime = ServiceLifetime.Scoped) where TContext : DbContext
{
//TODO: Somehow remember that this DbContext should be migrated.
return serviceCollection.AddDbContext<TContext, TContext>(optionsAction, contextLifetime, optionsLifetime);
}
用法:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddDbContextWithMigration<DbContext1>();
services.AddDbContext<DbContext2>();
services.AddDbContextWithMigration<DbContext3>();
}
然后,我认为我可以使用IStartupFilter
或为IApplicationBuilder
创建扩展名。
使用ExtensionMethod:
public static IApplicationBuilder RunMigrations(this IApplicationBuilder app)
{
if (app == null)
throw new ArgumentNullException(nameof(app));
var contexts = app.ApplicationServices.GetService();
foreach (DbContext context in contexts)
{
context.Database.Migrate();
}
return app;
}
使用ISTARTUPFILTER:
public class MigrationsFilter : IStartupFilter
{
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
{
return builder =>
{
var contexts = builder.ApplicationServices.GetService();
foreach (DbContext context in contexts)
{
context.Database.Migrate();
}
next(builder);
};
}
}
所以我基本上有两个问题。
- 如何跟踪应迁移的dbcontext?
- 这是
IStartupFilter
的"正确"使用?
我通过为我的迁移注册包装级(IContextMigrator
)解决了它。
首先,我的扩展方法:
public static IApplicationBuilder RunMigrations(this IApplicationBuilder app)
{
if (app == null)
throw new ArgumentNullException(nameof(app));
IEnumerable<IContextMigrator> migrations = app.ApplicationServices.GetServices<IContextMigrator>();
foreach (IContextMigrator migration in migrations)
{
migration.Migrate();
// Other logic for dispose...
}
return app;
}
public static IServiceCollection AddDbContextWithMigration<TContext>(this IServiceCollection serviceCollection, Action<DbContextOptionsBuilder> optionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Scoped, ServiceLifetime optionsLifetime = ServiceLifetime.Scoped) where TContext : DbContext
{
// By simply registering a transient of a wrapper-class I can resolve it later.
serviceCollection.AddTransient<IContextMigrator, ContextMigrator<TContext>>();
return serviceCollection.AddDbContext<TContext, TContext>(optionsAction, contextLifetime, optionsLifetime);
}
然后我的课程实际迁移:
public interface IContextMigrator : IDisposable
{
void Migrate();
}
/// <summary>
/// Class responsible for migration of a DbContext.
/// Used by ApplicationBuilderExtensions to perform migrations.
/// </summary>
/// <typeparam name="T"></typeparam>
internal sealed class ContextMigrator<T> : IContextMigrator
where T : DbContext
{
private readonly T context;
public ContextMigrator(T context)
{
this.context = context;
}
public void Migrate()
{
try
{
this.context.Database.Migrate();
}
catch (Exception e)
{
throw new MigrationException(context.GetType().Name, e);
}
}
public void Dispose()
{
}
}