1 Framework.Infrastructure.Extensions.ServiceCollectionExtensions. AddEFCoreContext
/// name="services">.Net(Core)框架内置依赖注入容器实例。
///
/// 【配置应用设定】
///
/// 摘要:
/// 抽离“EntityFrameworkCore”中间件实例的依赖注入操作,为当前程序通过“EntityFrameworkCore”中间件实例与指定数据库软件中指定数据库的CURD交互操作,提供实例支持。
///
///
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
///
/// 【异步已经生数据库?--无需权限】
///
///
/// 摘要:
/// 获取 1个值false(生成失败)/true(成功生成),该值指示是否通过“EntityFrameworkCore”中间件已经在指定数据库软件中成功生成了指定数据库及其表。
///
///
/// 返回:
/// 1个值false(生成失败)/true(成功生成)。
///
[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”中间件实例的依赖注入)。