第19章 抽离“EntityFrameworkCore”中间件实例的依赖注入
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
1 Framework.Infrastructure.Extensions.ServiceCollectionExtensions. AddEFCoreContext
/// <param name="services">.Net(Core)框架内置依赖注入容器实例。</param>
/// <summary>
/// 【配置应用设定】
/// <remarks>
/// 摘要
/// 抽离“EntityFrameworkCore”中间件实例的依赖注入操作为当前程序通过“EntityFrameworkCore”中间件实例与指定数据库软件中指定数据库的CURD交互操作提供实例支持。
/// </remarks>
/// </summary>
public static void AddEFCoreContext(this IServiceCollection services)
{
//从单例实例的字典成员实例中获取当前程序所有配置相关数据。
AppSettings _appSettings = Singleton<AppSettings>.Instance;
//从应用配置类实例中获取数据库连接相关数据。
DataConfig _dataConfig = _appSettings.Get<DataConfig>();
//说明如果想要“EntityFrameworkCore”中间件支持多数据库软件则把选择条件中的所有中间件都注入到依赖注入到.Net(Core)框架内置容器即可
//选择条件来限定当前程序只支持所设定的1个数据库软件当然“DataConfig”类与“appsettings.json”文件也必须为支持多数据库软件进行重构。
if (_dataConfig.DataProvider.ToString().Equals("sqlserver", StringComparison.InvariantCultureIgnoreCase))
{
//实例化“EntityFrameworkCore”中间件只支持“SqlServer”数据库软件与当前程序进行CURD交互操作。
//把“Microsoft.EntityFrameworkCore.SqlServer”中间件实例依赖注入到.Net(Core)框架内置容器中。
services.AddDbContext<EFCoreContext>(
//通过“DbContextOptionsBuilder”实例中的参数实例为“Microsoft.EntityFrameworkCore.SqlServer”中间件的实例化提供参数实例
//最终把“Microsoft.EntityFrameworkCore.SqlServer”中间件实例依赖注入到.Net(Core)框架内置容器中。
//IIS发布部署连接字符串必须使用“SQL Server身份认证”数据库连接方式才能实现发布部署程序与数据库的CURD的操作。
options => options.UseSqlServer(_dataConfig.ConnectionString));
}
else if (_dataConfig.DataProvider.ToString().Equals("mysql", StringComparison.InvariantCultureIgnoreCase))
{
//实例化“EntityFrameworkCore”中间件只支持“MySql”数据库软件与当前程序进行CURD交互操作。
//把“Microsoft.EntityFrameworkCore.SqlServer”中间件和“Pomelo.EntityFrameworkCore.MySql”实例依赖注入到.Net(Core)框架内置容器中。
services.AddDbContext<EFCoreContext>(
//实现“Microsoft.EntityFrameworkCore”中间件实例与“MySql”数据库的连接。
options => options.UseMySql(_dataConfig.ConnectionString, MySqlServerVersion.LatestSupportedServerVersion));
}
}
2 重构Program.cs文件
var builder = WebApplication.CreateBuilder(args);
//如果启动项中不存在“appsettings.json”文件则通过.Net(Core)的内置方法自动新建“appsettings.json”文件。
builder.Configuration.AddJsonFile("appsettings.json", true, true);
//把当前程序中所有继承了“IConfig”接口的具体实现类的实例依赖注入到.Net(Core)内置依赖注入容器实例中如果需要并把这些数据持久化存储到"appsettings.json"文件。
builder.Services.ConfigureApplicationSettings(builder);
builder.Services.AddScoped<INopFileProvider, NopFileProvider>();
//抽离“EntityFrameworkCore”中间件实例的依赖注入操作为当前程序通过“EntityFrameworkCore”中间件实例与指定数据库软件中指定数据库的CURD交互操作提供实例支持。
builder.Services.AddEFCoreContext();
3 WebApi.Controllers.MulDatabaseTestController.CreateDatabaseByEFCoreAsync
/// <summary>
/// 【异步已经生数据库--无需权限】
/// </summary>
/// <remarks>
/// 摘要
/// 获取 1个值false(生成失败)/true(成功生成)该值指示是否通过“EntityFrameworkCore”中间件已经在指定数据库软件中成功生成了指定数据库及其表。
/// </remarks>
/// <returns>
/// 返回
/// 1个值false(生成失败)/true(成功生成)。
/// </returns>
[HttpGet]
public async Task<MessageModel<bool>> CreateDatabaseByEFCoreAsync()
{
bool _isCreatedDatabase= await _context.Database.EnsureCreatedAsync();
MessageModel<bool> _messageModel = new MessageModel<bool>();
_messageModel.Response = _isCreatedDatabase;
if (_isCreatedDatabase)
{
_messageModel.Success = true;
_messageModel.Message = "已经成功在数据库软件中新建指定数据库及其表";
}
else
{
_messageModel.Success = false;
_messageModel.Message = "数据库软件中新建指定数据库及其表失败";
}
return _messageModel;
}
按F5执行程序不管在 “Microsoft SQL Server”数据库软件中还是在“MySql”数据库软件中都能自动生“ShopDemo” 数据库及其表。
对以上功能更为具体实现和注释见230117_013shopDemo(抽离“EntityFrameworkCore”中间件实例的依赖注入)。