feat: 移动 EFCore 配置到 Web 工程

This commit is contained in:
Argo-Tianyi 2021-12-15 16:02:53 +08:00
parent eaf36fec5e
commit ac2df1436d
2 changed files with 33 additions and 12 deletions

View File

@ -3,7 +3,6 @@ using BootstrapAdmin.DataAccess.EFCore.Services;
using BootstrapAdmin.Web.Core;
using BootstrapBlazor.Components;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace Microsoft.Extensions.DependencyInjection
{
@ -16,17 +15,33 @@ namespace Microsoft.Extensions.DependencyInjection
///
/// </summary>
/// <param name="services"></param>
/// <param name="optionConfigure"></param>
/// <param name="lifetime"></param>
/// <returns></returns>
public static IServiceCollection AddEFCoreDataAccessServices(this IServiceCollection services)
public static IServiceCollection AddEFCoreDataAccessServices(this IServiceCollection services, Action<IServiceProvider, DbContextOptionsBuilder> optionConfigure, ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
services.AddDbContextFactory<BootstrapAdminContext>((provider, option) =>
{
//TODO: 后期改造成自定适配
var configuration = provider.GetRequiredService<IConfiguration>();
var connString = configuration.GetConnectionString("bb");
option.UseSqlite(connString);
});
services.AddDbContextFactory<BootstrapAdminContext>(optionConfigure, lifetime);
services.AddServices();
return services;
}
/// <summary>
///
/// </summary>
/// <param name="services"></param>
/// <param name="optionConfigure"></param>
/// <param name="lifetime"></param>
/// <returns></returns>
public static IServiceCollection AddEFCoreDataAccessServices(this IServiceCollection services, Action<DbContextOptionsBuilder> optionConfigure, ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
services.AddDbContextFactory<BootstrapAdminContext>(optionConfigure, lifetime);
services.AddServices();
return services;
}
private static IServiceCollection AddServices(this IServiceCollection services)
{
// 增加数据服务
services.AddSingleton(typeof(IDataService<>), typeof(DefaultDataService<>));

View File

@ -1,5 +1,6 @@
using BootstrapAdmin.Web.Services;
using System.Text;
using Microsoft.EntityFrameworkCore;
namespace Microsoft.Extensions.DependencyInjection
{
@ -67,12 +68,17 @@ namespace Microsoft.Extensions.DependencyInjection
// 增加 BootstrapApp 上下文服务
services.AddScoped<BootstrapAppContext>();
// 增加 EFCore 数据服务
services.AddEFCoreDataAccessServices((provider, option) =>
{
var configuration = provider.GetRequiredService<IConfiguration>();
var connString = configuration.GetConnectionString("bb");
option.UseSqlite(connString);
});
// 增加 PetaPoco 数据服务
services.AddPetaPocoDataAccessServices();
// 增加 EFCore 数据服务
//services.AddEFCoreDataAccessServices();
return services;
}
}