简单的DbContext工厂类(EFCore)


前言

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



    }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM