From 0891d82d48a57df22e5b70ad2d54236815c76cba Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 18 Apr 2022 14:47:05 +0800 Subject: [PATCH] Add `AbpDesignTimeDbContextBase`. --- .../Design/AbpDesignTimeDbContextBase.cs | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Design/AbpDesignTimeDbContextBase.cs 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(); +}