From 46e98339480d853fc78a014c34d7ff9fcaf13890 Mon Sep 17 00:00:00 2001 From: dengjunjie <dengjunjie@hnkhzn.com> Date: 星期四, 05 十二月 2024 14:09:02 +0800 Subject: [PATCH] 产线协议 --- 项目代码/WMS/WIDESEA_WMSServer/WIDESEA_Core/Seed/DBSeed.cs | 81 ++++++++++++++++++++++++++-------------- 1 files changed, 53 insertions(+), 28 deletions(-) diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Seed/DBSeed.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Seed/DBSeed.cs" index b9b4568..4a92f35 100644 --- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Seed/DBSeed.cs" +++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Seed/DBSeed.cs" @@ -27,7 +27,7 @@ /// <param name="myContext"></param> /// <param name="WebRootPath"></param> /// <returns></returns> - public static async Task SeedAsync(DBContext dbContext, string WebRootPath) + public static void SeedAsync(DBContext dbContext, string WebRootPath) { try { @@ -40,9 +40,7 @@ Console.WriteLine("************ WIDESEA DataBase Set *****************"); - Console.WriteLine($"Master DB ConId: {DBContext.ConnId}"); Console.WriteLine($"Master DB Type: {DBContext.DbType}"); - //Console.WriteLine($"Master DB ConnectString: {DBContext.ConnectionString}"); Console.WriteLine(); @@ -52,7 +50,7 @@ if (DBContext.DbType != SqlSugar.DbType.Oracle) { dbContext.Db.DbMaintenance.CreateDatabase(); - ConsoleHelper.WriteSuccessLine($"Database created successfully!"); + ConsoleHelper.WriteSuccessLine($"Database Created Successfully!"); } else { @@ -65,20 +63,13 @@ Console.WriteLine("Create Tables..."); var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory; - var referencedAssemblies = System.IO.Directory.GetFiles(path, MainDb.AssemblyName).Select(Assembly.LoadFrom).ToArray(); + 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(); - //{ - // List<string> models = modelTypes.Select(x => x.Name).ToList(); - // List<DbTableInfo> dbTableInfos = dbContext.Db.DbMaintenance.GetTableInfoList(false); - // string[] dropTables = dbTableInfos.Select(x => x.Name).Where(x => !models.Contains(x)).ToArray(); - - // dbContext.Db.DbMaintenance.DropTable(dropTables); - //} modelTypes.ForEach(t => { @@ -88,28 +79,62 @@ IDbMaintenance dbMaintenance = dbContext.Db.DbMaintenance; if (!dbMaintenance.IsAnyTable(t.Name, false)) { - Console.WriteLine(t.Name); + ConsoleHelper.WriteSuccessLine($"Table [{t.Name}] Created Successfully"); dbContext.Db.CodeFirst.InitTables(t); - //dbContext.DbClient.QueryableByObject(t).ToList() == null + string seedData = FileHelper.ReadFile(string.Format(SeedDataFolder, t.Name), Encoding.UTF8); - - #region Sys_User + #region AddSeedData if (seedData != "涓嶅瓨鍦ㄧ浉搴旂殑鐩綍") { - List<Dictionary<string, object>> dic = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(seedData); - //dbContext.GetEntityDB<BlogArticle>().InsertRange(JsonHelper.ParseFormByJson<List<BlogArticle>>()); - //Console.WriteLine("Table:BlogArticle created success!"); + List<Dictionary<string, object>>? dicFile = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(seedData); - for (int i = 0; i < dic.Count; i++) + if (dicFile != null && dicFile.Count > 0) { - if (dic[i].ContainsKey("CreateDate")) - dic[i]["CreateDate"] = DateTime.Now; - else - dic[i].Add("CreateDate", DateTime.Now); - } + List<Dictionary<string, object>> dic = new List<Dictionary<string, object>>(); - dbContext.Db.Insertable(dic).AS(t.Name).ExecuteCommand(); + 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 } @@ -123,7 +148,7 @@ for (int i = 0; i < propertyInfos.Count; i++) { PropertyInfo propertyInfo = propertyInfos[i]; - SugarColumn sugarColumn = propertyInfo.GetCustomAttribute<SugarColumn>(); + SugarColumn? sugarColumn = propertyInfo.GetCustomAttribute<SugarColumn>(); if (sugarColumn != null) { if (!sugarColumn.IsIgnore) @@ -141,7 +166,7 @@ } } }); - ConsoleHelper.WriteSuccessLine($"Tables created successfully!"); + ConsoleHelper.WriteSuccessLine($"Tables Created Successfully!"); Console.WriteLine(); -- Gitblit v1.9.3