From cd174308cf81db9684183c91e227fdcf7feeb74c Mon Sep 17 00:00:00 2001
From: zyxucp <286513187@qq.com>
Date: Thu, 21 Mar 2024 12:20:05 +0800
Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E6=94=B9codefirst=E6=B3=A8?=
=?UTF-8?q?=E5=85=A5=E6=A8=A1=E5=BC=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/AntSK.Domain/AntSK.Domain.xml | 14 +++
.../DependencyInjection/InitExtensions.cs | 85 +++++++++++++++++++
src/AntSK/Program.cs | 44 +---------
3 files changed, 102 insertions(+), 41 deletions(-)
create mode 100644 src/AntSK.Domain/Common/DependencyInjection/InitExtensions.cs
diff --git a/src/AntSK.Domain/AntSK.Domain.xml b/src/AntSK.Domain/AntSK.Domain.xml
index 46ad881..0e8aefd 100644
--- a/src/AntSK.Domain/AntSK.Domain.xml
+++ b/src/AntSK.Domain/AntSK.Domain.xml
@@ -17,6 +17,20 @@
程序集集合
+
+
+ 使用codefirst创建数据库表
+
+
+
+
+
+
+ 加载数据库的插件
+
+
+
+
作用域
diff --git a/src/AntSK.Domain/Common/DependencyInjection/InitExtensions.cs b/src/AntSK.Domain/Common/DependencyInjection/InitExtensions.cs
new file mode 100644
index 0000000..9779faf
--- /dev/null
+++ b/src/AntSK.Domain/Common/DependencyInjection/InitExtensions.cs
@@ -0,0 +1,85 @@
+using AntSK.Domain.Domain.Service;
+using AntSK.Domain.Repositories;
+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;
+
+namespace AntSK.Domain.Common.DependencyInjection
+{
+ public static class InitExtensions
+ {
+ ///
+ /// 使用codefirst创建数据库表
+ ///
+ ///
+ ///
+ public static WebApplication CodeFirst(this WebApplication app)
+ {
+ using (var scope = app.Services.CreateScope())
+ {
+ // 获取仓储服务
+ var _repository = scope.ServiceProvider.GetRequiredService();
+
+ // 创建数据库(如果不存在)
+ _repository.GetDB().DbMaintenance.CreateDatabase();
+
+ // 获取当前应用程序域中所有程序集
+ var assemblies = AppDomain.CurrentDomain.GetAssemblies();
+
+ // 在所有程序集中查找具有[SugarTable]特性的类
+ foreach (var assembly in assemblies)
+ {
+ // 获取该程序集中所有具有SugarTable特性的类型
+ var entityTypes = assembly.GetTypes()
+ .Where(type => TypeIsEntity(type));
+
+ // 为每个找到的类型初始化数据库表
+ foreach (var type in entityTypes)
+ {
+ _repository.GetDB().CodeFirst.InitTables(type);
+ }
+ }
+ }
+ return app;
+ }
+
+
+ ///
+ /// 加载数据库的插件
+ ///
+ ///
+ ///
+ public static WebApplication LoadFun(this WebApplication app)
+ {
+ try
+ {
+ using (var scope = app.Services.CreateScope())
+ {
+ //codefirst 创建表
+ var funRep = scope.ServiceProvider.GetRequiredService();
+ var functionService = scope.ServiceProvider.GetRequiredService();
+ var funs = funRep.GetList();
+ foreach (var fun in funs)
+ {
+ functionService.FuncLoad(fun.Path);
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine(ex.Message + " ---- " + ex.StackTrace);
+ }
+ return app;
+ }
+ private static bool TypeIsEntity(Type type)
+ {
+ // 检查类型是否具有SugarTable特性
+ return type.GetCustomAttributes(typeof(SugarTable), inherit: false).Length > 0;
+ }
+ }
+}
diff --git a/src/AntSK/Program.cs b/src/AntSK/Program.cs
index 91bac20..9218722 100644
--- a/src/AntSK/Program.cs
+++ b/src/AntSK/Program.cs
@@ -132,8 +132,9 @@ if (!app.Environment.IsDevelopment())
app.UseStaticFiles();
-InitDB(app);
-LoadFun(app);
+//扩展初始化实现
+app.CodeFirst();
+app.LoadFun();
app.UseRouting();
@@ -150,43 +151,4 @@ app.UseEndpoints(endpoints =>
endpoints.MapControllers();
});
app.Run();
-void InitDB(WebApplication app)
-{
- using (var scope = app.Services.CreateScope())
- {
- //codefirst 创建表
- var _repository = scope.ServiceProvider.GetRequiredService();
- _repository.GetDB().DbMaintenance.CreateDatabase();
- _repository.GetDB().CodeFirst.InitTables(typeof(Apps));
- _repository.GetDB().CodeFirst.InitTables(typeof(Kmss));
- _repository.GetDB().CodeFirst.InitTables(typeof(KmsDetails));
- _repository.GetDB().CodeFirst.InitTables(typeof(Users));
- _repository.GetDB().CodeFirst.InitTables(typeof(Apis));
- _repository.GetDB().CodeFirst.InitTables(typeof(AIModels));
- _repository.GetDB().CodeFirst.InitTables(typeof(Funs));
- //创建vector插件如果数据库没有则需要提供支持向量的数据库
- _repository.GetDB().Ado.ExecuteCommandAsync($"CREATE EXTENSION IF NOT EXISTS vector;");
- }
-}
-void LoadFun(WebApplication app)
-{
- try
- {
- using (var scope = app.Services.CreateScope())
- {
- //codefirst 创建表
- var funRep = scope.ServiceProvider.GetRequiredService();
- var functionService = scope.ServiceProvider.GetRequiredService();
- var funs= funRep.GetList();
- foreach (var fun in funs)
- {
- functionService.FuncLoad(fun.Path);
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message + " ---- " + ex.StackTrace);
- }
-}
\ No newline at end of file