我正在为.NET Core应用程序开发一个可重用的小型程序包,该程序包将有助于在应用程序启动时自动迁移。
它基本上将在每个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
创建扩展方法。
使用扩展方法:
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);
};
}
}
所以我基本上有两个问题。
IStartupFilter
的“正确”用法吗?答案 0 :(得分:0)
我为迁移注册了包装类(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()
{
}
}