简单的DbContext工厂类(EFCore)
2024-10-19 03:14:48
前言
根据appsettings.json的中配置的数据库类型,使用工厂模式创建DbContext
代码实现
appsettings.json中的配置项
//使用的数据库类型
"ServerType": "oracle",
"OracleSettings": {
"PROTOCOL": "tcp",
"HOST": "XX.XX.XX.XX",
"PORT": "XX",
"SERVICE_NAME": "XX",
"UserId": "XX",
"Password": "XX"
},
"MySQLSettings": {
"HOST": "XX",
"PORT": "XX",
"DataBase": "XX",
"UserId": "XX",
"Password": "XX"
}
DbContext工厂
DbContextFactory
public class DbContextFactory
{
public static DbContext CreateContext()
{
DbContext dbContext = null;
string dbType = AppConfigurtaionServices.Configuration["ServerType"];
switch (dbType)
{
case "mysql":
dbContext = CreateMySQLContext();
break;
case "oracle":
dbContext = CreateOracleContext();
break;
}
return dbContext;
}
#region 创建dbContext
/// <summary>
/// 创建ORACLE Dbcontext
/// </summary>
/// <returns></returns>
static DbContext CreateOracleContext()
{
DbContextOptions<OracleContext> options = new DbContextOptions<OracleContext>();
OracleContext oracleContext = new OracleContext(options);
return oracleContext;
}
/// <summary>
/// 创建MySQL Dbcontext
/// </summary>
/// <returns></returns>
static DbContext CreateMySQLContext()
{
DbContextOptions<MySQLContext> options = new DbContextOptions<MySQLContext>();
MySQLContext mySQLContext = new MySQLContext(options);
return mySQLContext;
}
#endregion
}
AppConfigurtaionServices
public class AppConfigurtaionServices
{
public static IConfiguration Configuration { get; set; }
static AppConfigurtaionServices()
{
//ReloadOnChange = true 当appsettings.json被修改时重新加载
Configuration = new ConfigurationBuilder()
.Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
.Build();
}
}
OracleContext示例
public class OracleContext : DbContext
{
public OracleContext(DbContextOptions<OracleContext> options) : base(options)
{ }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string PROTOCOL = AppConfigurtaionServices.Configuration["OracleSettings:PROTOCOL"];
string HOST = AppConfigurtaionServices.Configuration["OracleSettings:HOST"];
string PORT = AppConfigurtaionServices.Configuration["OracleSettings:PORT"];
string SERVICE_NAME = AppConfigurtaionServices.Configuration["OracleSettings:SERVICE_NAME"];
string UserId = AppConfigurtaionServices.Configuration["OracleSettings:UserId"];
string Password = AppConfigurtaionServices.Configuration["OracleSettings:Password"];
optionsBuilder.UseOracle(
"Data Source = (DESCRIPTION = (ADDRESS = (PROTOCOL = " + PROTOCOL + ")(HOST = " + HOST + ")(PORT = " + PORT + "))(CONNECT_DATA = (SERVICE_NAME = " + SERVICE_NAME + "))); User Id = " + UserId + "; Password = " + Password + "; ");
base.OnConfiguring(optionsBuilder);
}
}
最新文章
- Yii2 modal中 ajax提交表单
- IIS6.0开启gzip压缩(来自百度)
- ruby -- 进阶学习(十五)friendly_id配置
- ASP.Net页面刷新后自动滚动到原来位置
- POJ2187Beauty Contest
- Java Math 类中的新功能--浮点数
- RESTful API学习与实践
- android studio sexy editor性感编辑器设置
- eclipse 用maven创建web项目
- SQL语句基本语法总结
- HTTP 错误 500.21 - Internal Server Error 处理程序“BlockViewHandler”在其模块列表中有一个错误模块“ManagedPipelineHandler
- Linux内核分析第五章读书笔记
- enode
- VS生成后事件对文件的copy以及更换扩展名
- java并发编程原理
- erlang中检查内存泄露
- time_t与GMT格式互转
- HTML+CSS-如何定义让两个div横向排列
- UITextField 限制用户输入小数点后位数的方法
- Atitit.数据库存储引擎的原理与attilax&#160;总结