Files
Yi.Admin/Yi.Framework.Net6/Yi.Framework.WebCore/DbExtend/DbSeedExtend.cs

97 lines
2.9 KiB
C#
Raw Normal View History

2022-09-19 17:25:43 +08:00
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
{
2022-09-25 20:41:55 +08:00
public static bool Invoer(ISqlSugarClient _Db)
2022-09-19 17:25:43 +08:00
{
2022-09-25 20:41:55 +08:00
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();
try
2022-09-19 17:25:43 +08:00
{
2022-09-25 20:41:55 +08:00
_Db.AsTenant().BeginTran();
2022-09-19 17:25:43 +08:00
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();
}
2022-09-25 18:06:07 +08:00
if (!_Db.Queryable<DictionaryEntity>().Any())
{
_Db.Insertable(dicts).ExecuteCommand();
}
2022-09-25 18:58:17 +08:00
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();
}
2022-09-25 18:06:07 +08:00
2022-09-19 17:25:43 +08:00
if (!_Db.Queryable<UserRoleEntity>().Any())
{
_Db.Insertable(SeedFactory.GetUserRoleSeed(users, roles)).ExecuteCommand();
}
if (!_Db.Queryable<RoleMenuEntity>().Any())
{
_Db.Insertable(SeedFactory.GetRoleMenuSeed(roles, menus)).ExecuteCommand();
}
2022-09-25 20:41:55 +08:00
_Db.AsTenant().CommitTran();
res = true;
}
catch (Exception ex)
{
_Db.AsTenant().RollbackTran();//数据回滚
Console.WriteLine(ex);
}
return res;
}
public static void UseDbSeedInitService(this IApplicationBuilder app)
{
2022-09-19 17:25:43 +08:00
2022-09-25 20:41:55 +08:00
if (Appsettings.appBool("DbSeed_Enabled"))
{
var _Db = app.ApplicationServices.GetService<ISqlSugarClient>();
Invoer(_Db);
2022-09-19 17:25:43 +08:00
}
}
}
}