/// <summary> /// 通过DbConnection对象创建DbConfiguration对象 /// </summary> /// <param name="conn"></param> /// <param name="entityAssembly"></param> /// <param name="containerName"></param> /// <param name="mappingFile"></param> /// <param name="isDefault"></param> /// <returns></returns> public static DbConfiguration Configure(string containerName, DbConnection conn, string entityAssembly, string mappingFile, bool isDefault) { Guard.NotNull(conn, "conn"); var providerType = conn.GetType() .Assembly .GetTypes() .Where(t => typeof(System.Data.Common.DbProviderFactory).IsAssignableFrom(t) && t.Namespace == conn.GetType().Namespace) .FirstOrDefault() ; if (providerType == null) { throw new NotSupportedException("not found 'DbProviderFactory'"); } var factory = providerType.GetField("Instance", BindingFlags.Public | BindingFlags.Static).GetValue(null) as DbProviderFactory; Guard.NotNull(factory, "factory"); var providerName = providerType.Namespace; var cfg = new DbConfiguration(providerName, containerName, conn.ConnectionString, isDefault, factory); if (!Options.ContainsKey(providerName)) { var dbtype = conn.GetType().Name; if (dbtype.StartsWith("MySql")) { providerName = DbProviderNames.MySQL; } else if (dbtype.StartsWith("SqlCe")) { providerName = DbProviderNames.SqlCe35; } else if (dbtype.StartsWith("Oledb")) { providerName = DbProviderNames.Oledb; } else if (dbtype.StartsWith("Oracle")) { providerName = DbProviderNames.Oracle; } else if (dbtype.StartsWith("SQLite")) { providerName = DbProviderNames.SQLite; } else if (dbtype.StartsWith("System.Data.SqlClient.")) { providerName = DbProviderNames.SqlServer; } } if (!string.IsNullOrEmpty(mappingFile)) { cfg.AddFile(mappingFile); } else if (!string.IsNullOrEmpty(entityAssembly)) { AddMappingClass(Assembly.Load(entityAssembly), cfg); } else { AddMappingClass(Assembly.GetExecutingAssembly(), cfg); } PopulateSqlServer2000(conn, factory, cfg); cfg.connection = conn; return(cfg); }
/// <summary> /// 通过connectionStringName对象创建DbConfiguration对象(可以用于配置文件中有多个数据库连接字符串配置) /// </summary> /// <param name="dbContextOption"></param> /// <returns></returns> public static DbConfiguration Configure(DbContextOption dbContextOption) { Guard.NotNullOrEmpty(dbContextOption.Container, "connectionStringName"); DbConfiguration cfg; if (items.TryGetValue(dbContextOption.Container, out cfg)) { if (cfg.sqlLogger == null) { cfg.SetSqlLogger(() => dbContextOption.LogggerFactory.CreateLogger(dbContextOption.Container)); } return(cfg); } //var item = UCloudConfiguration.Current.GetConnectionString(connectionStringName); if (string.IsNullOrEmpty(dbContextOption.Provider)) { throw new ConfigurationErrorsException("connectionString.ProviderName"); } var connectionString = dbContextOption.ConnectionString; if (string.IsNullOrEmpty(connectionString)) { throw new ConfigurationErrorsException("ConnectionString"); } DbProviderFactory factory = null; try { factory = DbProviderFactoriesEx.GetFactory(dbContextOption.Provider); } catch (Exception ex) { throw new ConfigurationErrorsException(dbContextOption.Provider + " Provider name invalid for" + dbContextOption.Container, ex); } cfg = new DbConfiguration(dbContextOption.Provider, dbContextOption.Container, connectionString, dbContextOption.Default, factory); if (!string.IsNullOrEmpty(dbContextOption.MappingFile)) { cfg.AddFile(dbContextOption.MappingFile); } else if (!string.IsNullOrEmpty(dbContextOption.ModuleAssemblyName)) { var assembly = Assembly.Load(dbContextOption.ModuleAssemblyName); var types = assembly?.GetTypes(); var list = types?.Where(t => t.IsClass && !t.IsGenericType && !t.IsAbstract && t.GetAttribute <TableAttribute>() != null).ToList(); if (list != null && list.Any()) { list.ForEach(t => { cfg.AddClass(t); }); } } cfg.SetSqlLogger(() => dbContextOption.LogggerFactory.CreateLogger(dbContextOption.Container)); lock (items) items[cfg.Name] = cfg; AutoMatchDialect(cfg, connectionString, dbContextOption.Provider, factory); if (dbContextOption.ConnectionString != null) { cfg.MakeDefault(); } return(cfg); }