添加codeFirst功能

This commit is contained in:
陈淳
2022-10-18 18:01:16 +08:00
parent dab4a092d9
commit ab8cdd88b9
25 changed files with 161 additions and 121 deletions

View File

@@ -13,7 +13,12 @@ namespace Yi.Framework.WebCore.DbExtend
{
public static class DbSeedExtend
{
public static bool Invoer(ISqlSugarClient _Db)
/// <summary>
/// 初始化数据
/// </summary>
/// <param name="_Db"></param>
/// <returns></returns>
public static bool DataInvoer(ISqlSugarClient _Db)
{
bool res = false;
var users = SeedFactory.GetUserSeed();
@@ -87,14 +92,40 @@ namespace Yi.Framework.WebCore.DbExtend
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>();
Invoer(_Db);
DataInvoer(_Db);
}
}