You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
5.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using zzz.Common;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace zzz.Services
{
/// <summary>
/// 种子数据 服务
/// </summary>
public class DBSeed: IDBSeed
{
private readonly ISqlSugarClient _sqlSugarClient;
private static string SeedDataFolder = "BlogCore.Data.json/{0}.tsv";
/// <summary>
/// 构造
/// </summary>
/// <param name="sqlSugarClient"></param>
public DBSeed(ISqlSugarClient sqlSugarClient)
{
_sqlSugarClient = sqlSugarClient;
}
/// <summary>
/// 异步添加种子数据
/// </summary>
/// <param name="myContext"></param>
/// <param name="WebRootPath"></param>
/// <returns></returns>
public async static Task SeedAsync(ISqlSugarClient myContext,string WebRootPath)
{
try
{
if (string.IsNullOrEmpty(WebRootPath))
{
throw new Exception("获取wwwroot路径时异常");
}
//创建数据库
if (Appsettings.app("SeedDBEnabled").ObjToBool())
{
var dbtype = Appsettings.app("DBType").ObjToInt();
Console.WriteLine($"Create Database...");
if (dbtype != Convert.ToInt32(SqlSugar.DbType.Oracle))
{
myContext.DbMaintenance.CreateDatabase();
ConsoleHelper.WriteSuccessLine($"Database created successfully!");
}
else
{
//Oracle 数据库不支持该操作
ConsoleHelper.WriteSuccessLine($"Oracle 数据库不支持该操作可手动创建Oracle数据库!");
}
}
//初始化数据
if (Appsettings.app("SeedDBDataEnabled").ObjToBool())
{
// 创建数据库表遍历指定命名空间下的class
// 注意不要把其他命名空间下的也添加进来。
Console.WriteLine("Create Tables...");
var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
var referencedAssemblies = System.IO.Directory.GetFiles(path, "zzz.Model.dll").Select(Assembly.LoadFrom).ToArray();
var modelTypes = referencedAssemblies
.SelectMany(a => a.DefinedTypes)
.Select(type => type.AsType())
.Where(x => x.IsClass && x.Namespace != null && x.Namespace.Equals("zzz.Model.Entity")).ToList();
modelTypes.ForEach(t =>
{
// 这里只支持添加表,不支持删除
// 如果想要删除数据库直接右键删除或者联系SqlSugar作者
if (!myContext.DbMaintenance.IsAnyTable(t.Name))
{
Console.WriteLine(t.Name);
myContext.CodeFirst.InitTables(t);
}
});
ConsoleHelper.WriteSuccessLine($"Tables created successfully!");
Console.WriteLine();
}
Console.WriteLine();
}
catch (Exception ex)
{
throw new Exception(
$"1、若是Mysql,查看常见问题:https://github.com/anjoy8/zzz/issues/148#issue-776281770 \n" +
$"2、若是Oracle,查看常见问题:https://github.com/anjoy8/zzz/issues/148#issuecomment-752340231 \n" +
"3、其他错误" + ex.Message);
}
}
/// <summary>
/// 同步表结构
/// </summary>
/// <param name="tableName"></param>
/// <returns></returns>
public async Task CreateTable(string tableName)
{
if (tableName.IsNotEmptyOrNull())
{
try
{
var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
var referencedAssemblies = System.IO.Directory.GetFiles(path, "zzz.Model.dll").Select(Assembly.LoadFrom).ToArray();
var modelTypes = referencedAssemblies
.SelectMany(a => a.DefinedTypes)
.Select(type => type.AsType())
.Where(x => x.IsClass && x.Namespace != null && x.Namespace.Equals("zzz.Model.Entity") && x.Name == tableName)
.FirstOrDefault();
_sqlSugarClient.CodeFirst.InitTables(modelTypes);
}
catch (Exception ex)
{
throw;
}
}
}
}
}