#region << 版 本 注 释 >>
|
/*----------------------------------------------------------------
|
* 命名空间:WIDESEAWCS_QuartzJob
|
* 创建者:胡童庆
|
* 创建时间:2024/8/2 16:13:36
|
* 版本:V1.0.0
|
* 描述:映射QuartzJob数据库表
|
*
|
* ----------------------------------------------------------------
|
* 修改人:
|
* 修改时间:
|
* 版本:V1.0.1
|
* 修改说明:
|
*
|
*----------------------------------------------------------------*/
|
#endregion << 版 本 注 释 >>
|
|
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
|
{
|
/// <summary>
|
/// 映射QuartzJob数据库表
|
/// </summary>
|
/// <param name="dbContext"></param>
|
/// <returns></returns>
|
public static async Task SeedAsync(DBContext dbContext)
|
{
|
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<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))
|
{
|
Console.WriteLine(t.Name);
|
dbContext.Db.CodeFirst.InitTables(t);
|
}
|
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($"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);
|
}
|
}
|
}
|
}
|