/// <summary> /// 根据配置文件获取数据库类型,并返回对应的工厂接口 /// </summary> /// <param name="conString">链接字符串</param> /// <param name="dbType">数据库类型</param> /// <returns></returns> public static IRepository GetRepository(string conString = null, DatabaseType?dbType = null) { conString = conString.IsNullOrEmpty() ? GlobalSwitch.DefaultDbConName : conString; conString = DbProviderFactoryHelper.GetConStr(conString); dbType = dbType.IsNullOrEmpty() ? GlobalSwitch.DatabaseType : dbType; Type dbRepositoryType = Type.GetType("Coldairarrow.DataRepository." + DbProviderFactoryHelper.DbTypeToDbTypeStr(dbType.Value) + "Repository"); return(Activator.CreateInstance(dbRepositoryType, new object[] { conString }) as IRepository); }
/// <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); }
/// <summary> /// 根据配置文件获取数据库类型,并返回对应的工厂接口 /// </summary> /// <param name="conString">链接字符串</param> /// <param name="dbType">数据库类型</param> /// <returns></returns> public static IRepository GetRepository(string conString = null, DatabaseType?dbType = null) { conString = conString.IsNullOrEmpty() ? GlobalSwitch.DefaultDbConName : conString; conString = DbProviderFactoryHelper.GetConStr(conString); dbType = dbType.IsNullOrEmpty() ? GlobalSwitch.DatabaseType : dbType; Type dbRepositoryType = Type.GetType("Coldairarrow.DataRepository." + DbProviderFactoryHelper.DbTypeToDbTypeStr(dbType.Value) + "Repository"); var repository = Activator.CreateInstance(dbRepositoryType, new object[] { conString }) as IRepository; //请求结束自动释放 try { AutofacHelper.GetScopeService <IDisposableContainer>().AddDisposableObj(repository); } catch { } return(repository); }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (_nameOrConStr.IsNullOrEmpty()) { _nameOrConStr = GlobalSwitch.DefaultDbConName; } string conStr = DbProviderFactoryHelper.GetConStr(_nameOrConStr); switch (_dbType) { case DatabaseType.SqlServer: optionsBuilder.UseSqlServer(_nameOrConStr).EnableSensitiveDataLogging(); break; case DatabaseType.MySql: optionsBuilder.UseMySQL(_nameOrConStr); break; default: throw new Exception("暂不支持该数据库!"); } optionsBuilder.UseLoggerFactory(_loger); }