private static void AddMappingClass(Assembly assembly, DbConfiguration cfg) { 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); }); } }
/// <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); }