数据库种子初始化搭建

This commit is contained in:
陈淳
2022-09-19 17:25:43 +08:00
parent e8bb256a8d
commit 5785f5beea
8 changed files with 265 additions and 30 deletions

View File

@@ -0,0 +1,56 @@
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
{
public static void UseDbSeedInitService(this IApplicationBuilder app)
{
if (Appsettings.appBool("DbSeed_Enabled"))
{
var _Db = app.ApplicationServices.GetService<ISqlSugarClient>();
var users = SeedFactory.GetUserSeed();
var roles = SeedFactory.GetRoleSeed();
var menus = SeedFactory.GetMenuSeed();
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<UserRoleEntity>().Any())
{
_Db.Insertable(SeedFactory.GetUserRoleSeed(users, roles)).ExecuteCommand();
}
if (!_Db.Queryable<RoleMenuEntity>().Any())
{
_Db.Insertable(SeedFactory.GetRoleMenuSeed(roles, menus)).ExecuteCommand();
}
}
}
}
}