| | |
| | | *----------------------------------------------------------------*/ |
| | | #endregion << 版 本 注 释 >> |
| | | |
| | | using Newtonsoft.Json; |
| | | using SqlSugar; |
| | | using System; |
| | | using System.Collections.Generic; |
| | |
| | | { |
| | | public class QuartzJobCreateDataTabel |
| | | { |
| | | private static string SeedDataFolder = "WIDESEAWCS_DB.DBSeed.Json/{0}.tsv"; |
| | | |
| | | /// <summary> |
| | | /// 映射QuartzJob数据库表 |
| | | /// </summary> |
| | | /// <param name="dbContext"></param> |
| | | /// <returns></returns> |
| | | public static async Task SeedAsync(DBContext dbContext) |
| | | public static async Task SeedAsync(DBContext dbContext, string WebRootPath) |
| | | { |
| | | try |
| | | { |
| | |
| | | .Select(type => type.AsType()) |
| | | .Where(x => x.IsClass && x.Namespace is "WIDESEAWCS_QuartzJob.Models" && x.GetCustomAttribute<SugarTable>() != null) |
| | | .ToList(); |
| | | |
| | | SeedDataFolder = Path.Combine(WebRootPath, SeedDataFolder); |
| | | modelTypes.ForEach(t => |
| | | { |
| | | //var diffString = dbContext.Db.CodeFirst.GetDifferenceTables(t).ToDiffString(); |
| | |
| | | { |
| | | Console.WriteLine(t.Name); |
| | | dbContext.Db.CodeFirst.InitTables(t); |
| | | } |
| | | else |
| | | |
| | | 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.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(); |
| | | if (t.GetProperties().FirstOrDefault(x => !columnNames.Contains(x.Name)) != null) |
| | | 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++) |
| | | { |
| | | bool isChange = true; |
| | | List<PropertyInfo> propertyInfos = t.GetProperties().Where(x => !columnNames.Contains(x.Name)).ToList(); |
| | | Dictionary<string, object> keyValuePairs = new Dictionary<string, object>(); |
| | | for (int i = 0; i < propertyInfos.Count; i++) |
| | | { |
| | | PropertyInfo propertyInfo = propertyInfos[i]; |
| | |
| | | { |
| | | if (!sugarColumn.IsIgnore) |
| | | { |
| | | if (!sugarColumn.IsNullable) |
| | | keyValuePairs.Add(propertyInfo.Name, dicFile[j][propertyInfo.Name]); |
| | | } |
| | | } |
| | | } |
| | | dic.Add(keyValuePairs); |
| | | } |
| | | |
| | | if (dic.Count > 0) |
| | | { |
| | | isChange = false; |
| | | break; |
| | | 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"); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | if (isChange) |
| | | dbContext.Db.CodeFirst.InitTables(t); |
| | | } |
| | | #endregion |
| | | } |
| | | }); |
| | | ConsoleHelper.WriteSuccessLine($"QuartzJob Tables Created Successfully!"); |