/// <summary> /// 获取 /// </summary> /// <param name="conString"></param> /// <param name="dbType"></param> /// <returns></returns> internal static BaseDbContext GetDbContext([NotNull] string conString, DatabaseType dbType) { if (conString.IsNullOrEmpty()) { throw new Exception("conString能为空"); } var dbConnection = DbProviderFactoryHelper.GetDbConnection(conString, dbType); var model = DbModelFactory.GetDbCompiledModel(conString, dbType); DbContextOptionsBuilder builder = new DbContextOptionsBuilder(); switch (dbType) { case DatabaseType.SqlServer: builder.UseSqlServer(dbConnection, x => x.UseRowNumberForPaging()); break; case DatabaseType.MySql: builder.UseMySql(dbConnection); break; case DatabaseType.PostgreSql: builder.UseNpgsql(dbConnection); break; case DatabaseType.Oracle: builder.UseOracle(dbConnection, x => x.UseOracleSQLCompatibility("11")); break; default: throw new Exception("暂不支持该数据库!"); } builder.EnableSensitiveDataLogging(); builder.UseModel(model); builder.UseLoggerFactory(_loggerFactory); return(new BaseDbContext(builder.Options)); }
/// <summary> /// 获取DbConnection /// </summary> /// <param name="conStr">连接名或字符串</param> /// <returns></returns> private static DbConnection GetDbConnection(string conStr, DatabaseType dbType) { if (conStr.IsNullOrEmpty()) { conStr = GlobalSwitch.DefaultDbConName; } DbConnection dbConnection = DbProviderFactoryHelper.GetDbConnection(dbType); dbConnection.ConnectionString = DbProviderFactoryHelper.GetConStr(conStr); return(dbConnection); }
private void RefreshDb() { var oldDb = _db; var con = DbProviderFactoryHelper.GetDbConnection(_conString, _dbType); var dBCompiledModel = DbModelFactory.GetDbCompiledModel(_conString, _dbType); _db = new BaseDbContext(con, dBCompiledModel); if (oldDb != null) { _db.Database.Log += oldDb.Database.Log; } }
public void RefreshDb() { //重用DbConnection,使用底层相同的DbConnection,支持Model持热更新 DbConnection con = null; if (_transaction != null) { con = _transaction.Connection; } else { con = _db?.Database?.GetDbConnection() ?? DbProviderFactoryHelper.GetDbConnection(_conString, _dbType); } var dBCompiledModel = DbModelFactory.GetDbCompiledModel(_conString, _dbType); _db = new BaseDbContext(_dbType, con, dBCompiledModel); _db.Database.UseTransaction(_transaction); disposedValue = false; }
private static DbCompiledModelInfo BuildDbCompiledModelInfo(string nameOrConStr, DatabaseType dbType) { lock (_buildCompiledModelLock) { DbConnection connection = DbProviderFactoryHelper.GetDbConnection(nameOrConStr, dbType); DbModelBuilder modelBuilder = new DbModelBuilder(DbModelBuilderVersion.Latest); modelBuilder.HasDefaultSchema(GetSchema()); var entityMethod = typeof(DbModelBuilder).GetMethod("Entity"); _modelTypeMap.Values.ToList().ForEach(aModel => { entityMethod.MakeGenericMethod(aModel).Invoke(modelBuilder, null); }); var theModel = modelBuilder.Build(connection).Compile(); DbCompiledModelInfo info = new DbCompiledModelInfo { ConStr = connection.ConnectionString, DatabaseType = dbType, DbCompiledModel = theModel }; return(info); string GetSchema() { switch (dbType) { case DatabaseType.SqlServer: return("dbo"); case DatabaseType.MySql: case DatabaseType.PostgreSql: return("public"); case DatabaseType.Oracle: return(new OracleConnectionStringBuilder(connection.ConnectionString).UserID);; default: return("dbo"); } } } }