/// <summary> /// 得到一个 DbContextOptionsBuilder 对象 /// </summary> /// <param name="optionsBuilder">初始化该对象</param> /// <returns></returns> public static DbContextOptionsBuilder <ContextBase> DCOB(DbContextOptionsBuilder optionsBuilder = null) { DbContextOptionsBuilder <ContextBase> cb = null; if (optionsBuilder == null) { cb = new DbContextOptionsBuilder <ContextBase>(); optionsBuilder = cb; } if (!optionsBuilder.IsConfigured) { System.Enum.TryParse(GlobalTo.GetValue("TypeDB"), true, out TDB); switch (TDB) { case TypeDB.MySQL: optionsBuilder.UseMySql(GlobalTo.Configuration.GetConnectionString(TDB.ToString())); break; case TypeDB.SQLite: optionsBuilder.UseSqlite(GlobalTo.Configuration.GetConnectionString(TDB.ToString()).Replace("~", GlobalTo.ContentRootPath)); break; case TypeDB.InMemory: optionsBuilder.UseInMemoryDatabase(GlobalTo.Configuration.GetConnectionString(TDB.ToString())); break; case TypeDB.SQLServer: optionsBuilder.UseSqlServer(GlobalTo.Configuration.GetConnectionString(TDB.ToString()), options => { //启用 row_number 分页 (兼容2005、2008) //options.UseRowNumberForPaging(); }); break; case TypeDB.PostgreSQL: optionsBuilder.UseNpgsql(GlobalTo.Configuration.GetConnectionString(TDB.ToString())); break; } //注册日志(修改日志等级为Information,可查看执行的SQL语句) optionsBuilder.UseLoggerFactory(LoggerFactory); } return(cb); }
/// <summary> /// 配置连接字符串 /// </summary> /// <param name="optionsBuilder"></param> protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { switch (TDB) { case TypeDB.MySQL: optionsBuilder.UseMySql(GlobalTo.Configuration.GetConnectionString(TDB.ToString())); break; case TypeDB.SQLite: optionsBuilder.UseSqlite(GlobalTo.Configuration.GetConnectionString(TDB.ToString()).Replace("~", GlobalTo.ContentRootPath)); break; case TypeDB.InMemory: optionsBuilder.UseInMemoryDatabase(GlobalTo.Configuration.GetConnectionString(TDB.ToString())); break; case TypeDB.SQLServer: optionsBuilder.UseSqlServer(GlobalTo.Configuration.GetConnectionString(TDB.ToString()), options => { //启用 row_number 分页 (兼容2005、2008) //options.UseRowNumberForPaging(); }); break; case TypeDB.PostgreSQL: optionsBuilder.UseNpgsql(GlobalTo.Configuration.GetConnectionString(TDB.ToString())); break; } //注册日志(修改日志等级为Information,可查看执行的SQL语句) optionsBuilder.UseLoggerFactory(LoggerFactory); } }
/// <summary> /// 根据数据库类型获取连接字符串 /// </summary> /// <returns></returns> public static string GetConn() { return(Configuration.GetConnectionString(TDB.ToString())); }