feat:完成codefirst与种子数据,发现非空类型问题,等待紧急修复
This commit is contained in:
@@ -26,23 +26,23 @@ namespace Yi.Framework.Infrastructure.Data.DataSeeds
|
||||
{
|
||||
//using (var uow = iUnitOfWorkManager.CreateContext())
|
||||
//{
|
||||
var res = await iUnitOfWorkManager.Ado.UseTranAsync(async () =>
|
||||
{
|
||||
//var res = await iUnitOfWorkManager.Ado.UseTranAsync(async () =>
|
||||
// {
|
||||
foreach (var seed in dataSeeds)
|
||||
{
|
||||
await seed.InvokerAsync();
|
||||
}
|
||||
});
|
||||
//});
|
||||
|
||||
//var res = uow.Commit();
|
||||
|
||||
if (!res.IsSuccess)
|
||||
{
|
||||
throw new ApplicationException("种子数据初始化异常");
|
||||
}
|
||||
//if (!res.IsSuccess)
|
||||
//{
|
||||
//throw new ApplicationException("种子数据初始化异常");
|
||||
//}
|
||||
//}
|
||||
}
|
||||
return builder.UseMiddleware<DataFilterMiddleware>();
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Furion;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SqlSugar;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.Sqlsugar
|
||||
{
|
||||
public static class SqlsugarCodeFirstExtensions
|
||||
{
|
||||
public static void UseSqlsugarCodeFirstServer(this IApplicationBuilder app)
|
||||
{
|
||||
var db = app.ApplicationServices.GetRequiredService<ISqlSugarClient>();
|
||||
var options = app.ApplicationServices.GetRequiredService<IOptions<DbConnOptions>>();
|
||||
|
||||
if (options.Value.EnabledCodeFirst == false) return;
|
||||
|
||||
db.DbMaintenance.CreateDatabase();
|
||||
var assemblys = new List<Assembly>();
|
||||
|
||||
//全盘加载
|
||||
if (options.Value.EntityAssembly is null)
|
||||
{
|
||||
assemblys.AddRange(App.Assemblies.ToList());
|
||||
}
|
||||
//按需加载
|
||||
else
|
||||
{
|
||||
options.Value.EntityAssembly.ForEach(a =>
|
||||
{
|
||||
assemblys.Add(Assembly.Load(a));
|
||||
});
|
||||
}
|
||||
|
||||
foreach (var assembly in assemblys)
|
||||
{
|
||||
TableInvoer(db, assembly.GetTypes().ToList());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void TableInvoer(ISqlSugarClient _Db, List<Type> typeList)
|
||||
{
|
||||
foreach (var t in typeList)
|
||||
{
|
||||
//扫描如果存在SugarTable特性 并且 不是分表模型,直接codefirst
|
||||
if (t.GetCustomAttributes(false).Any(a => a.GetType().Equals(typeof(SugarTable))
|
||||
&& !t.GetCustomAttributes(false).Any(a => a.GetType().Equals(typeof(SplitTableAttribute)))))
|
||||
{
|
||||
_Db.CodeFirst.SetStringDefaultLength(200).InitTables(t);//这样一个表就能成功创建了
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ using StackExchange.Profiling.SqlFormatters;
|
||||
using Yi.Framework.Infrastructure.AspNetCore;
|
||||
using Yi.Framework.Infrastructure.Auth;
|
||||
using Yi.Framework.Infrastructure.Data;
|
||||
using Yi.Framework.Infrastructure.Data.DataSeeds;
|
||||
using Yi.Framework.Infrastructure.Data.Filters;
|
||||
using Yi.Framework.Infrastructure.Sqlsugar;
|
||||
using Yi.Framework.Infrastructure.Sqlsugar.Filters;
|
||||
@@ -31,13 +32,17 @@ public class Startup : AppStartup
|
||||
|
||||
services.AddSingleton<IPermissionHandler, DefaultPermissionHandler>();
|
||||
services.AddSingleton<PermissionGlobalAttribute>();
|
||||
services.AddControllers(options => {
|
||||
services.AddControllers(options =>
|
||||
{
|
||||
options.Filters.Add<PermissionGlobalAttribute>();
|
||||
});
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
public async void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
app.UseSqlsugarCodeFirstServer();
|
||||
await app.UseDataSeedServer();
|
||||
app.UseDataFiterServer();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user