Browse Source

Add `AbpDesignTimeDbContextBase`.

pull/12289/head
maliming 5 years ago
parent
commit
0891d82d48
No known key found for this signature in database GPG Key ID: 96224957E51C89E
  1. 29
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Design/AbpDesignTimeDbContextBase.cs

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

@ -0,0 +1,29 @@
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>();
application.Services.ReplaceConfiguration(BuildConfiguration());
await application.InitializeAsync();
return application.ServiceProvider.GetRequiredService<TContext>();
}
protected abstract IConfigurationRoot BuildConfiguration();
}
Loading…
Cancel
Save