Files
Yi.Admin/Yi.Framework.Net6/Yi.Framework.WebCore/DbExtend/DbSeedExtend.cs
2022-10-18 18:01:16 +08:00

134 lines
4.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Model.Models;
using Yi.Framework.Model.SeedData;
namespace Yi.Framework.WebCore.DbExtend
{
public static class DbSeedExtend
{
/// <summary>
/// 初始化数据
/// </summary>
/// <param name="_Db"></param>
/// <returns></returns>
public static bool DataInvoer(ISqlSugarClient _Db)
{
bool res = false;
var users = SeedFactory.GetUserSeed();
var roles = SeedFactory.GetRoleSeed();
var menus = SeedFactory.GetMenuSeed();
var dicts = SeedFactory.GetDictionarySeed();
var posts = SeedFactory.GetPostSeed();
var dictinfos = SeedFactory.GetDictionaryInfoSeed();
var depts = SeedFactory.GetDeptSeed();
var files = SeedFactory.GetFileSeed();
try
{
_Db.AsTenant().BeginTran();
if (!_Db.Queryable<UserEntity>().Any())
{
_Db.Insertable(users).ExecuteCommand();
}
if (!_Db.Queryable<RoleEntity>().Any())
{
_Db.Insertable(roles).ExecuteCommand();
}
if (!_Db.Queryable<MenuEntity>().Any())
{
_Db.Insertable(menus).ExecuteCommand();
}
if (!_Db.Queryable<DictionaryEntity>().Any())
{
_Db.Insertable(dicts).ExecuteCommand();
}
if (!_Db.Queryable<PostEntity>().Any())
{
_Db.Insertable(posts).ExecuteCommand();
}
if (!_Db.Queryable<DictionaryInfoEntity>().Any())
{
_Db.Insertable(dictinfos).ExecuteCommand();
}
if (!_Db.Queryable<DeptEntity>().Any())
{
_Db.Insertable(depts).ExecuteCommand();
}
if (!_Db.Queryable<UserRoleEntity>().Any())
{
_Db.Insertable(SeedFactory.GetUserRoleSeed(users, roles)).ExecuteCommand();
}
if (!_Db.Queryable<RoleMenuEntity>().Any())
{
_Db.Insertable(SeedFactory.GetRoleMenuSeed(roles, menus)).ExecuteCommand();
}
if (!_Db.Queryable<FileEntity>().Any())
{
_Db.Insertable(files).ExecuteCommand();
}
_Db.AsTenant().CommitTran();
res = true;
}
catch (Exception ex)
{
_Db.AsTenant().RollbackTran();//数据回滚
Console.WriteLine(ex);
}
return res;
}
/// <summary>
/// codeFirst初始化表
/// </summary>
/// <param name="_Db"></param>
/// <returns></returns>
public static void TableInvoer(ISqlSugarClient _Db)
{
//创建数据库
_Db.DbMaintenance.CreateDatabase();
var typeList = Common.Helper.AssemblyHelper.GetClass("Yi.Framework.Model");
foreach (var t in typeList)
{
//扫描如果存在SugarTable特性直接codefirst
if (t.GetCustomAttributes(false).Any(a => a.GetType().Equals(typeof(SugarTable))))
{
_Db.CodeFirst.SetStringDefaultLength(200).InitTables(t);//这样一个表就能成功创建了
}
}
}
public static void UseDbSeedInitService(this IApplicationBuilder app)
{
if (Appsettings.appBool("DbCodeFirst_Enabled"))
{
var _Db = app.ApplicationServices.GetRequiredService<ISqlSugarClient>();
TableInvoer(_Db);
}
if (Appsettings.appBool("DbSeed_Enabled"))
{
var _Db = app.ApplicationServices.GetRequiredService<ISqlSugarClient>();
DataInvoer(_Db);
}
}
}
}