#region << 版 本 注 释 >> /*---------------------------------------------------------------- * 命名空间:WIDESEAWCS_QuartzJob * 创建者:胡童庆 * 创建时间:2024/8/2 16:13:36 * 版本:V1.0.0 * 描述:映射QuartzJob数据库表 * * ---------------------------------------------------------------- * 修改人: * 修改时间: * 版本:V1.0.1 * 修改说明: * *----------------------------------------------------------------*/ #endregion << 版 本 注 释 >> using Newtonsoft.Json; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using WIDESEAWCS_Core.DB; using WIDESEAWCS_Core.Helper; using WIDESEAWCS_Core.Seed; namespace WIDESEAWCS_QuartzJob.Seed { public class QuartzJobCreateDataTabel { private static string SeedDataFolder = "WIDESEAWCS_DB.DBSeed.Json/{0}.tsv"; /// /// 映射QuartzJob数据库表 /// /// /// public static async Task SeedAsync(DBContext dbContext, string WebRootPath) { try { Console.WriteLine("Create QuartzJob Tables..."); var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory; var referencedAssemblies = System.IO.Directory.GetFiles(path, "WIDESEAWCS_QuartzJob.dll").Select(Assembly.LoadFrom).ToArray(); var modelTypes = referencedAssemblies .SelectMany(a => a.DefinedTypes) .Select(type => type.AsType()) .Where(x => x.IsClass && x.Namespace is "WIDESEAWCS_QuartzJob.Models" && x.GetCustomAttribute() != null) .ToList(); SeedDataFolder = Path.Combine(WebRootPath, SeedDataFolder); modelTypes.ForEach(t => { //var diffString = dbContext.Db.CodeFirst.GetDifferenceTables(t).ToDiffString(); // 这里只支持添加表,不支持删除 // 如果想要删除,数据库直接右键删除,或者联系SqlSugar作者; IDbMaintenance dbMaintenance = dbContext.Db.DbMaintenance; if (!dbMaintenance.IsAnyTable(t.Name, false)) { Console.WriteLine(t.Name); dbContext.Db.CodeFirst.InitTables(t); string seedData = FileHelper.ReadFile(string.Format(SeedDataFolder, t.Name), Encoding.UTF8); #region AddSeedData if (seedData != "不存在相应的目录") { List> dicFile = JsonConvert.DeserializeObject>>(seedData); if (dicFile.Count > 0) { List> dic = new List>(); List columnNames = dbContext.Db.DbMaintenance.GetColumnInfosByTableName(t.Name, false).Select(x => x.DbColumnName).ToList(); var a = t.GetProperties().FirstOrDefault(x => !columnNames.Contains(x.Name)); List propertyInfos = t.GetProperties().Where(x => columnNames.Contains(x.Name)).ToList(); for (int j = 0; j < dicFile.Count; j++) { Dictionary keyValuePairs = new Dictionary(); for (int i = 0; i < propertyInfos.Count; i++) { PropertyInfo propertyInfo = propertyInfos[i]; SugarColumn sugarColumn = propertyInfo.GetCustomAttribute(); 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 } }); ConsoleHelper.WriteSuccessLine($"QuartzJob 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); } } } }