¶Ô±ÈÐÂÎļþ |
| | |
| | | using Castle.Components.DictionaryAdapter.Xml; |
| | | using Microsoft.AspNetCore.Mvc.Rendering; |
| | | using Newtonsoft.Json; |
| | | using SqlSugar; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Dynamic; |
| | | using System.Linq; |
| | | using System.Net.Sockets; |
| | | using System.Reflection; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | using WIDESEA_Core.Const; |
| | | using WIDESEA_Core.DB; |
| | | using WIDESEA_Core.Helper; |
| | | using WIDESEA_Core.Tenants; |
| | | |
| | | namespace WIDESEA_Core.Seed |
| | | { |
| | | public class DBSeed |
| | | { |
| | | private static string SeedDataFolder = "WIDESEA_DB.DBSeed.Json/{0}.tsv"; |
| | | |
| | | /// <summary> |
| | | /// 弿¥æ·»å ç§åæ°æ® |
| | | /// </summary> |
| | | /// <param name="myContext"></param> |
| | | /// <param name="WebRootPath"></param> |
| | | /// <returns></returns> |
| | | public static void SeedAsync(DBContext dbContext, string WebRootPath) |
| | | { |
| | | try |
| | | { |
| | | if (string.IsNullOrEmpty(WebRootPath)) |
| | | { |
| | | throw new Exception("è·åwwwrootè·¯å¾æ¶ï¼å¼å¸¸ï¼"); |
| | | } |
| | | |
| | | SeedDataFolder = Path.Combine(WebRootPath, SeedDataFolder); |
| | | |
| | | Console.WriteLine("************ WIDESEA DataBase Set *****************"); |
| | | |
| | | Console.WriteLine($"Master DB Type: {DBContext.DbType}"); |
| | | |
| | | Console.WriteLine(); |
| | | |
| | | // åå»ºæ°æ®åº |
| | | Console.WriteLine($"Create Database(The Db Id:{DBContext.ConnId})..."); |
| | | |
| | | if (DBContext.DbType != SqlSugar.DbType.Oracle) |
| | | { |
| | | dbContext.Db.DbMaintenance.CreateDatabase(); |
| | | ConsoleHelper.WriteSuccessLine($"Database Created Successfully!"); |
| | | } |
| | | else |
| | | { |
| | | //Oracle æ°æ®åºä¸æ¯æè¯¥æä½ |
| | | ConsoleHelper.WriteSuccessLine($"Oracle æ°æ®åºä¸æ¯æè¯¥æä½ï¼å¯æå¨å建Oracleæ°æ®åº!"); |
| | | } |
| | | |
| | | // åå»ºæ°æ®åºè¡¨ï¼éåæå®å½å空é´ä¸çclassï¼ |
| | | // 注æä¸è¦æå
¶ä»å½å空é´ä¸ç乿·»å è¿æ¥ã |
| | | Console.WriteLine("Create Tables..."); |
| | | |
| | | var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory; |
| | | var referencedAssemblies = Directory.GetFiles(path, MainDb.AssemblyName).Select(Assembly.LoadFrom).ToArray(); |
| | | |
| | | var modelTypes = referencedAssemblies |
| | | .SelectMany(a => a.DefinedTypes) |
| | | .Select(type => type.AsType()) |
| | | .Where(x => x.IsClass && x.Namespace is MainDb.EntityNameSpace && x.GetCustomAttribute<SugarTable>() != null) |
| | | .ToList(); |
| | | |
| | | modelTypes.ForEach(t => |
| | | { |
| | | //var diffString = dbContext.Db.CodeFirst.GetDifferenceTables(t).ToDiffString(); |
| | | // è¿éåªæ¯ææ·»å 表ï¼ä¸æ¯æå é¤ |
| | | // 妿æ³è¦å é¤ï¼æ°æ®åºç´æ¥å³é®å é¤ï¼æè
èç³»SqlSugarä½è
ï¼ |
| | | IDbMaintenance dbMaintenance = dbContext.Db.DbMaintenance; |
| | | if (!dbMaintenance.IsAnyTable(t.Name, false)) |
| | | { |
| | | ConsoleHelper.WriteSuccessLine($"Table [{t.Name}] Created Successfully"); |
| | | dbContext.Db.CodeFirst.InitTables(t); |
| | | |
| | | string seedData = FileHelper.ReadFile(string.Format(SeedDataFolder, t.Name), Encoding.UTF8); |
| | | |
| | | #region AddSeedData |
| | | if (seedData != "ä¸åå¨ç¸åºçç®å½") |
| | | { |
| | | List<Dictionary<string, object>>? dicFile = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(seedData); |
| | | |
| | | if (dicFile != null && dicFile.Count > 0) |
| | | { |
| | | List<Dictionary<string, object>> dic = new List<Dictionary<string, object>>(); |
| | | |
| | | List<string> columnNames = dbContext.Db.DbMaintenance.GetColumnInfosByTableName(t.Name, false).Select(x => x.DbColumnName).ToList(); |
| | | var a = t.GetProperties().FirstOrDefault(x => !columnNames.Contains(x.Name)); |
| | | |
| | | List<PropertyInfo> propertyInfos = t.GetProperties().Where(x => columnNames.Contains(x.Name)).ToList(); |
| | | for (int j = 0; j < dicFile.Count; j++) |
| | | { |
| | | Dictionary<string, object> keyValuePairs = new Dictionary<string, object>(); |
| | | for (int i = 0; i < propertyInfos.Count; i++) |
| | | { |
| | | PropertyInfo propertyInfo = propertyInfos[i]; |
| | | SugarColumn? sugarColumn = propertyInfo.GetCustomAttribute<SugarColumn>(); |
| | | if (sugarColumn != null) |
| | | { |
| | | if (!sugarColumn.IsIgnore) |
| | | { |
| | | keyValuePairs.Add(propertyInfo.Name, dicFile[j][propertyInfo.Name]); |
| | | } |
| | | } |
| | | } |
| | | dic.Add(keyValuePairs); |
| | | } |
| | | |
| | | if (dic.Count > 0) |
| | | { |
| | | for (int i = 0; i < dic.Count; i++) |
| | | { |
| | | if (dic[i].ContainsKey("CreateDate")) |
| | | dic[i]["CreateDate"] = DateTime.Now; |
| | | else |
| | | dic[i].Add("CreateDate", DateTime.Now); |
| | | } |
| | | string str = $"SET IDENTITY_INSERT {t.Name} ON;"; |
| | | |
| | | str += dbContext.Db.Insertable(dic).AS(t.Name).ToSqlString(); |
| | | |
| | | str += ($"SET IDENTITY_INSERT {t.Name} OFF;"); |
| | | |
| | | dbContext.Db.Ado.ExecuteCommand(str); |
| | | |
| | | ConsoleHelper.WriteSuccessLine($"Table [{t.Name}] SeedData Added Successfully"); |
| | | } |
| | | } |
| | | } |
| | | #endregion |
| | | } |
| | | else |
| | | { |
| | | List<string> columnNames = dbContext.Db.DbMaintenance.GetColumnInfosByTableName(t.Name, false).Select(x => x.DbColumnName).ToList(); |
| | | if (t.GetProperties().FirstOrDefault(x => !columnNames.Contains(x.Name)) != null) |
| | | { |
| | | bool isChange = true; |
| | | List<PropertyInfo> propertyInfos = t.GetProperties().Where(x => !columnNames.Contains(x.Name)).ToList(); |
| | | for (int i = 0; i < propertyInfos.Count; i++) |
| | | { |
| | | PropertyInfo propertyInfo = propertyInfos[i]; |
| | | SugarColumn? sugarColumn = propertyInfo.GetCustomAttribute<SugarColumn>(); |
| | | if (sugarColumn != null) |
| | | { |
| | | if (!sugarColumn.IsIgnore) |
| | | { |
| | | if (!sugarColumn.IsNullable) |
| | | { |
| | | isChange = false; |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | if (isChange) |
| | | dbContext.Db.CodeFirst.InitTables(t); |
| | | } |
| | | } |
| | | }); |
| | | ConsoleHelper.WriteSuccessLine($"Tables Created Successfully!"); |
| | | Console.WriteLine(); |
| | | |
| | | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | // 1ãè¥æ¯Mysql,æ¥ç常è§é®é¢:https://github.com/anjoy8/Blog.Core/issues/148#issue-776281770 |
| | | //2ãè¥æ¯Oracle,æ¥ç常è§é®é¢:https://github.com/anjoy8/Blog.Core/issues/148#issuecomment-752340231 |
| | | throw new Exception("é误ï¼" + ex.Message); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// åå§å å¤ç§æ· |
| | | /// </summary> |
| | | /// <param name="dbContext"></param> |
| | | /// <returns></returns> |
| | | public static async Task TenantSeedAsync(DBContext dbContext) |
| | | { |
| | | |
| | | if (BaseDBConfig.MutiConnectionString.Where(x => x.ConnId != MainDb.CurrentDbConnId).Any()) |
| | | { |
| | | Console.WriteLine($@"Init Multi Tenant Db"); |
| | | foreach (MutiDBOperate tenant in BaseDBConfig.MutiConnectionString.Where(x => x.ConnId != MainDb.CurrentDbConnId)) |
| | | { |
| | | |
| | | Console.WriteLine($@"Init Multi Tenant Db : {tenant.ConnId}"); |
| | | ConnectionConfig connectionConfig = new ConnectionConfig() |
| | | { |
| | | ConfigId = tenant.ConnId, |
| | | ConnectionString = tenant.Connection, |
| | | IsAutoCloseConnection = true, |
| | | MoreSettings = new ConnMoreSettings() |
| | | { |
| | | IsAutoRemoveDataCache = true |
| | | }, |
| | | DbType = (DbType)tenant.DbType, |
| | | }; |
| | | await InitTenantSeedAsync(dbContext.Db.AsTenant(), connectionConfig); |
| | | } |
| | | |
| | | Console.WriteLine(DateTime.Now + $@"Init Multi Tenant Db Finish"); |
| | | } |
| | | |
| | | //tenants = await myContext.Db.Queryable<SysTenant>().Where(s => s.TenantType == TenantTypeEnum.Tables).ToListAsync(); |
| | | //if (tenants.Any()) |
| | | //{ |
| | | // await InitTenantSeedAsync(myContext, tenants); |
| | | //} |
| | | } |
| | | |
| | | #region å¤ç§æ· å¤åº åå§å |
| | | |
| | | /// <summary> |
| | | /// åå§åå¤åº |
| | | /// </summary> |
| | | /// <param name="itenant"></param> |
| | | /// <param name="config"></param> |
| | | /// <returns></returns> |
| | | public static async Task InitTenantSeedAsync(ITenant itenant, ConnectionConfig config) |
| | | { |
| | | Console.WriteLine(DateTime.Now + $@"Init Multi Tenant Db"); |
| | | //itenant.RemoveConnection(config.ConfigId); |
| | | itenant.AddConnection(config); |
| | | |
| | | var db = itenant.GetConnectionScope(config.ConfigId); |
| | | |
| | | db.DbMaintenance.CreateDatabase(); |
| | | ConsoleHelper.WriteSuccessLine($"Init Multi Tenant Db : {config.ConfigId} Database created successfully!"); |
| | | |
| | | Console.WriteLine($@"Init Multi Tenant Db : {config.ConfigId} Create Tables"); |
| | | |
| | | // è·åææå®ä½è¡¨-åå§åç§æ·ä¸å¡è¡¨ |
| | | var entityTypes = TenantUtil.GetTenantEntityTypes(TenantTypeEnum.Db); |
| | | if (!entityTypes.Any()) return; |
| | | foreach (var entityType in entityTypes) |
| | | { |
| | | //var splitTable = entityType.GetCustomAttribute<SplitTableAttribute>(); |
| | | //if (splitTable == null) |
| | | db.CodeFirst.InitTables(entityType); |
| | | //else |
| | | // db.CodeFirst.SplitTables().InitTables(entityType); |
| | | |
| | | Console.WriteLine(entityType.Name); |
| | | } |
| | | Console.WriteLine(DateTime.Now + $@"Init Multi Tenant Db Finish"); |
| | | //å¤ç§æ·åå§åç§åæ°æ® |
| | | //await TenantSeedDataAsync(db, TenantTypeEnum.Db); |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | //private static async Task TenantSeedDataAsync(ISqlSugarClient db, TenantTypeEnum tenantType) |
| | | //{ |
| | | // // è·åææç§åé
ç½®-åå§åæ°æ® |
| | | // var seedDataTypes = AssemblysExtensions.GetAllAssemblies().SelectMany(s => s.DefinedTypes) |
| | | // .Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass) |
| | | // .Where(u => |
| | | // { |
| | | // var esd = u.GetInterfaces().FirstOrDefault(i => i.HasImplementedRawGeneric(typeof(IEntitySeedData<>))); |
| | | // if (esd is null) |
| | | // { |
| | | // return false; |
| | | // } |
| | | |
| | | // var eType = esd.GenericTypeArguments[0]; |
| | | // return eType.IsTenantEntity(tenantType); |
| | | // }); |
| | | // if (!seedDataTypes.Any()) return; |
| | | // foreach (var seedType in seedDataTypes) |
| | | // { |
| | | // dynamic instance = Activator.CreateInstance(seedType); |
| | | // //åå§åæ°æ® |
| | | // { |
| | | // var seedData = instance.InitSeedData(); |
| | | // if (seedData != null && Enumerable.Any(seedData)) |
| | | // { |
| | | // var entityType = seedType.GetInterfaces().First().GetGenericArguments().First(); |
| | | // var entity = db.EntityMaintenance.GetEntityInfo(entityType); |
| | | |
| | | // if (!await db.Queryable(entity.DbTableName, "").AnyAsync()) |
| | | // { |
| | | // await db.Insertable(Enumerable.ToList(seedData)).ExecuteCommandAsync(); |
| | | // Console.WriteLine($"Table:{entity.DbTableName} init success!"); |
| | | // } |
| | | // } |
| | | // } |
| | | |
| | | // //ç§åæ°æ® |
| | | // { |
| | | // var seedData = instance.SeedData(); |
| | | // if (seedData != null && Enumerable.Any(seedData)) |
| | | // { |
| | | // var entityType = seedType.GetInterfaces().First().GetGenericArguments().First(); |
| | | // var entity = db.EntityMaintenance.GetEntityInfo(entityType); |
| | | |
| | | // await db.Storageable(Enumerable.ToList(seedData)).ExecuteCommandAsync(); |
| | | // Console.WriteLine($"Table:{entity.DbTableName} seedData success!"); |
| | | // } |
| | | // } |
| | | |
| | | // //èªå®ä¹å¤ç |
| | | // { |
| | | // await instance.CustomizeSeedData(db); |
| | | // } |
| | | // } |
| | | //} |
| | | } |
| | | } |