通過(guò)屬性區(qū)分業(yè)務(wù)表 與主庫(kù)的區(qū)別
/// <summary> /// 初始化數(shù)據(jù)庫(kù) /// </summary> /// <param name="db"></param> /// <param name="config"></param> private static void InitDatabase(SqlSugarScope db, DbConnectionConfig config) { SqlSugarScopeProvider dbProvider = db.GetConnectionScope(config.ConfigId); // 初始化/創(chuàng)建數(shù)據(jù)庫(kù) if (config.DbSettings.EnableInitDb) { if (config.DbType != SqlSugar.DbType.Oracle) dbProvider.DbMaintenance.CreateDatabase(); } // 初始化表結(jié)構(gòu) if (config.TableSettings.EnableInitTable) { var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false)) .WhereIF(config.TableSettings.EnableIncreTable, u => u.IsDefined(typeof(IncreTableAttribute), false)).ToList(); //var infos1 = entityTypes.Where(u => u.GetCustomAttributes<SysTableAttribute>().Any()).ToList(); // var infos = entityTypes.Where(u => u.GetCustomAttributes<TenantAttribute>().Any()).ToList(); if (config.ConfigId.ToString() == SqlSugarConst.MainConfigId) // 默認(rèn)庫(kù)(有系統(tǒng)表特性、沒(méi)有日志表和租戶表特性) entityTypes = entityTypes.Where(u => u.GetCustomAttributes<SysTableAttribute>().Any()/* || (!u.GetCustomAttributes<LogTableAttribute>().Any() && !u.GetCustomAttributes<TenantAttribute>().Any())*/).ToList(); else if (config.ConfigId.ToString() == SqlSugarConst.LogConfigId) // 日志庫(kù) entityTypes = entityTypes.Where(u => u.GetCustomAttributes<LogTableAttribute>().Any()).ToList(); else entityTypes = entityTypes.Where(u => u.GetCustomAttribute<TenantAttribute>()?.configId.ToString() == config.ConfigId.ToString()).ToList(); // 自定義的庫(kù) foreach (var entityType in entityTypes) { if (entityType.GetCustomAttribute<SplitTableAttribute>() == null) dbProvider.CodeFirst.InitTables(entityType); else dbProvider.CodeFirst.SplitTables().InitTables(entityType); } } // 初始化種子數(shù)據(jù) if (config.SeedSettings.EnableInitSeed) { var seedDataTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>)))) .WhereIF(config.SeedSettings.EnableIncreSeed, u => u.IsDefined(typeof(IncreSeedAttribute), false)).ToList(); foreach (var seedType in seedDataTypes) { var entityType = seedType.GetInterfaces().First().GetGenericArguments().First(); if (config.ConfigId.ToString() == SqlSugarConst.MainConfigId) // 默認(rèn)庫(kù)(有系統(tǒng)表特性、沒(méi)有日志表和租戶表特性) { if (entityType.GetCustomAttribute<SysTableAttribute>() == null && (entityType.GetCustomAttribute<LogTableAttribute>() != null || entityType.GetCustomAttribute<TenantAttribute>() != null)) continue; } else if (config.ConfigId.ToString() == SqlSugarConst.LogConfigId) // 日志庫(kù) { if (entityType.GetCustomAttribute<LogTableAttribute>() == null) continue; } else { var att = entityType.GetCustomAttribute<TenantAttribute>(); // 自定義的庫(kù) if (att == null || att.configId.ToString() != config.ConfigId.ToString()) continue; } var instance = Activator.CreateInstance(seedType); var hasDataMethod = seedType.GetMethod("HasData"); var seedData = ((IEnumerable)hasDataMethod?.Invoke(instance, null))?.Cast<object>(); if (seedData == null) continue; var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(entityType); if (entityInfo.Columns.Any(u => u.IsPrimarykey)) { // 按主鍵進(jìn)行批量增加和更新 var storage = dbProvider.StorageableByObject(seedData.ToList()).ToStorage(); storage.AsInsertable.ExecuteCommand(); storage.AsUpdateable.ExecuteCommand(); } else { // 無(wú)主鍵則只進(jìn)行插入 if (!dbProvider.Queryable(entityInfo.DbTableName, entityInfo.DbTableName).Any()) dbProvider.InsertableByObject(seedData.ToList()).ExecuteCommand(); } } } }
技術(shù)交流qq群:143280841
浙公網(wǎng)安備 33010602011771號(hào)