diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Design/AbpDesignTimeDbContextBase.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Design/AbpDesignTimeDbContextBase.cs new file mode 100644 index 0000000000..c8fce37399 --- /dev/null +++ b/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 : IDesignTimeDbContextFactory + where TModule : AbpModule + where TContext : DbContext +{ + public virtual TContext CreateDbContext(string[] args) + { + return AsyncHelper.RunSync(() => CreateDbContextAsync(args)); + } + + protected virtual async Task CreateDbContextAsync(string[] args) + { + var application = await AbpApplicationFactory.CreateAsync(); + application.Services.ReplaceConfiguration(BuildConfiguration()); + await application.InitializeAsync(); + return application.ServiceProvider.GetRequiredService(); + } + + protected abstract IConfigurationRoot BuildConfiguration(); +}