通过属性区分业务表 与主库的区别
/// <summary> /// 初始化数据库 /// </summary> /// <param name="db"></param> /// <param name="config"></param> private static void InitDatabase(SqlSugarScope db, DbConnectionConfig config) { SqlSugarScopeProvider dbProvider = db.GetConnectionScope(config.ConfigId); // 初始化/创建数据库 if (config.DbSettings.EnableInitDb) { if (config.DbType != SqlSugar.DbType.Oracle) dbProvider.DbMaintenance.CreateDatabase(); } // 初始化表结构 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) // 默认库(有系统表特性、没有日志表和租户表特性) entityTypes = entityTypes.Where(u => u.GetCustomAttributes<SysTableAttribute>().Any()/* || (!u.GetCustomAttributes<LogTableAttribute>().Any() && !u.GetCustomAttributes<TenantAttribute>().Any())*/).ToList(); else if (config.ConfigId.ToString() == SqlSugarConst.LogConfigId) // 日志库 entityTypes = entityTypes.Where(u => u.GetCustomAttributes<LogTableAttribute>().Any()).ToList(); else entityTypes = entityTypes.Where(u => u.GetCustomAttribute<TenantAttribute>()?.configId.ToString() == config.ConfigId.ToString()).ToList(); // 自定义的库 foreach (var entityType in entityTypes) { if (entityType.GetCustomAttribute<SplitTableAttribute>() == null) dbProvider.CodeFirst.InitTables(entityType); else dbProvider.CodeFirst.SplitTables().InitTables(entityType); } } // 初始化种子数据 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) // 默认库(有系统表特性、没有日志表和租户表特性) { if (entityType.GetCustomAttribute<SysTableAttribute>() == null && (entityType.GetCustomAttribute<LogTableAttribute>() != null || entityType.GetCustomAttribute<TenantAttribute>() != null)) continue; } else if (config.ConfigId.ToString() == SqlSugarConst.LogConfigId) // 日志库 { if (entityType.GetCustomAttribute<LogTableAttribute>() == null) continue; } else { var att = entityType.GetCustomAttribute<TenantAttribute>(); // 自定义的库 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)) { // 按主键进行批量增加和更新 var storage = dbProvider.StorageableByObject(seedData.ToList()).ToStorage(); storage.AsInsertable.ExecuteCommand(); storage.AsUpdateable.ExecuteCommand(); } else { // 无主键则只进行插入 if (!dbProvider.Queryable(entityInfo.DbTableName, entityInfo.DbTableName).Any()) dbProvider.InsertableByObject(seedData.ToList()).ExecuteCommand(); } } } }
技术交流qq群:143280841