Browse Source

Merge pull request #12289 from abpframework/AbpDesignTimeDbContextBase

Add `AbpDesignTimeDbContextBase`.
pull/12312/head
liangshiwei 4 years ago
committed by GitHub
parent
commit
2fbfe30a37
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 39
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Design/AbpDesignTimeDbContextBase.cs

39
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Design/AbpDesignTimeDbContextBase.cs

@ -0,0 +1,39 @@
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;
using Volo.Abp.Threading;
namespace Volo.Abp.EntityFrameworkCore.Design;
public abstract class AbpDesignTimeDbContextBase<TModule, TContext> : IDesignTimeDbContextFactory<TContext>
where TModule : AbpModule
where TContext : DbContext
{
public virtual TContext CreateDbContext(string[] args)
{
return AsyncHelper.RunSync(() => CreateDbContextAsync(args));
}
protected virtual async Task<TContext> CreateDbContextAsync(string[] args)
{
var application = await AbpApplicationFactory.CreateAsync<TModule>(options =>
{
options.Services.ReplaceConfiguration(BuildConfiguration());
ConfigureServices(options.Services);
});
await application.InitializeAsync();
return application.ServiceProvider.GetRequiredService<TContext>();
}
protected virtual void ConfigureServices(IServiceCollection services)
{
}
protected abstract IConfigurationRoot BuildConfiguration();
}
Loading…
Cancel
Save