前言

根据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);
} }

最新文章

  1. Yii2 modal中 ajax提交表单
  2. IIS6.0开启gzip压缩(来自百度)
  3. ruby -- 进阶学习(十五)friendly_id配置
  4. ASP.Net页面刷新后自动滚动到原来位置
  5. POJ2187Beauty Contest
  6. Java Math 类中的新功能--浮点数
  7. RESTful API学习与实践
  8. android studio sexy editor性感编辑器设置
  9. eclipse 用maven创建web项目
  10. SQL语句基本语法总结
  11. HTTP 错误 500.21 - Internal Server Error 处理程序“BlockViewHandler”在其模块列表中有一个错误模块“ManagedPipelineHandler
  12. Linux内核分析第五章读书笔记
  13. enode
  14. VS生成后事件对文件的copy以及更换扩展名
  15. java并发编程原理
  16. erlang中检查内存泄露
  17. time_t与GMT格式互转
  18. HTML+CSS-如何定义让两个div横向排列
  19. UITextField 限制用户输入小数点后位数的方法
  20. Atitit.数据库存储引擎的原理与attilax&#160;总结

热门文章

  1. day019python之面向对象基础1
  2. .NET Core 3.0或3.1 类库项目中引用 Microsoft.AspNetCore.App
  3. git初尝
  4. 【electron+vue3+ts实战便笺exe】二、electron+vue3开发内容
  5. CentOS7下常用安装服务软件源码编译安装方式的介绍
  6. Spring AOP 实战运用
  7. Layui关闭弹出层对话框--刷新父界面
  8. 解析STM32的库函数
  9. 【vue】常用指令
  10. informix部署安装