From 193068f7b5eed5c5722c16b31ec170564c455031 Mon Sep 17 00:00:00 2001
From: liaoxujun@qq.com <liaoxujun@qq.com>
Date: 星期三, 26 七月 2023 15:45:42 +0800
Subject: [PATCH]  jwt 配置,codefist  sqlsug数据服务注入

---
 DocumentServiceAPI.Core/DbContext.cs |   87 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 86 insertions(+), 1 deletions(-)

diff --git a/DocumentServiceAPI.Core/DbContext.cs b/DocumentServiceAPI.Core/DbContext.cs
index 235a7fe..2aaa3fd 100644
--- a/DocumentServiceAPI.Core/DbContext.cs
+++ b/DocumentServiceAPI.Core/DbContext.cs
@@ -1,6 +1,16 @@
-锘縰sing Furion;
+锘縰sing DocumentServiceAPI.Enum;
+using DocumentServiceAPI.Utility;
+using Furion;
+using Furion.Logging;
+using Furion.Logging.Extensions;
+using Microsoft.AspNetCore.Builder;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.DependencyInjection;
 using SqlSugar;
+using System;
 using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
 
 namespace DocumentServiceAPI.Core;
 
@@ -18,5 +28,80 @@
         , db =>
         {
             // 杩欓噷閰嶇疆鍏ㄥ眬浜嬩欢锛屾瘮濡傛嫤鎴墽琛� SQL
+           
+           
         });
+    public static void AddSqlsugarSetup(this IServiceCollection services)
+    {
+        var configConnection = App.GetConfig<List<ConnectionConfig>>("ConnectionConfigs");
+        
+
+
+        services.AddSingleton<ISqlSugarClient>(s =>
+        {
+            SqlSugarScope sqlSugar = new SqlSugarScope(configConnection,
+           db =>
+           {
+               db.CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices()
+               {
+                   //鍒ゆ柇鏄惁寮�鍚痳edis璁剧疆浜岀骇缂撳瓨鏂瑰紡
+                 //  DataInfoCacheService = new SqlSugarRedisCache(),
+                   //妯″瀷瀹氫箟涓� int?鍙锋椂鑷姩涓哄彲绌�
+                   EntityService = (c, p) =>
+                   {
+                       /***楂樼増C#鍐欐硶***/
+                       //鏀寔string?鍜宻tring  
+                       if (p.IsPrimarykey == false && new NullabilityInfoContext()
+                        .Create(c).WriteState is NullabilityState.Nullable)
+                       {
+                           p.IsNullable = true;
+                       }
+                   }
+
+               };
+               db.CurrentConnectionConfig.MoreSettings = new ConnMoreSettings()
+               {
+                   //鎵�鏈� 澧炪�佸垹 銆佹敼 浼氳嚜鍔ㄨ皟鐢�.RemoveDataCache()娓呯悊浜岀骇缂撳瓨
+                   IsAutoRemoveDataCache = true
+               };
+               db.Aop.OnError = (exp) =>
+               {
+                   $"鏁版嵁搴撴墽琛岄敊璇簡{exp}".LogInformation();
+                //   NlogUtil.WriteFileLog(NLog.LogLevel.Error, LogType.Other, "SqlSugar", "鎵цSQL閿欒浜嬩欢", exp);
+               };
+
+               //鍗曚緥鍙傛暟閰嶇疆锛屾墍鏈変笂涓嬫枃鐢熸晥
+               //db.Aop.OnLogExecuting = (sql, pars) =>
+               //{
+               //    //鑾峰彇浣淚OC浣滅敤鍩熷璞�
+               //  //  var appServive = s.GetService<IHttpContextAccessor>();
+               //   // var obj = appServive?.HttpContext?.RequestServices.GetService<Log>();
+               // //   Console.WriteLine("AOP" + obj.GetHashCode());
+               //};
+           });
+            return sqlSugar;
+        });
+    }
+
+    /// <summary>
+    /// 鏍规嵁鍛藉悕绌洪棿鍜屾暟鎹簱杩炴帴key濉〃
+    /// 娉ㄦ剰锛屽彧鏈夌户鎵夸簡ezBaseModel 鐨勬ā鍨嬫墠浼氬垱寤�
+    /// </summary>
+    /// <param name="app"></param>
+    /// <param name="ConfigId">閰嶇疆搴撳悕</param>
+    /// <param name="ModeProjectName"></param>
+    public static void DbCodeFirst(this IApplicationBuilder app, string ModeProjectName, string? Modenamespace = null, string? ConfigId = null)
+    {
+       var ddb= App.GetService<ISqlSugarClient>();
+        var _db = app.ApplicationServices.GetService<ISqlSugarClient>();
+        //鍒囨崲鏁版嵁搴�
+        if (!string.IsNullOrEmpty(ConfigId))
+            _db.AsTenant().ChangeDatabase(ConfigId);
+        //寤哄簱
+        _db.DbMaintenance.CreateDatabase();
+        Type[]? types = UtilityFun.GetAllAssembly().Where(x => x.FullName.Contains(ModeProjectName + ",")).FirstOrDefault()?.GetTypes().WhereIF(!string.IsNullOrEmpty(Modenamespace), name => name.FullName.Contains(Modenamespace + ".")).ToArray().Where(x => x.IsSubclassOf(typeof(BaseModel))).ToArray();
+
+        _db.CodeFirst.InitTables(types);//鏍规嵁types鍒涘缓琛�
+
+    }
 }

--
Gitblit v1.9.1