移动系统liao
2025-01-26 950f32a006736bd9407bb5f73b744892b8bade2a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
 
using Furion;
using Furion.Logging.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
 
namespace CYZuoYeBenPeiSong.Core
{
    public class Startup : AppStartup
    {
        //使用sqlsugar 数据库连接库
 
        public void ConfigureServices(IServiceCollection services)
        {
            var configConnection = App.GetConfig<List<ConnectionConfig>>("DbConnect");
 
 
            services.AddSingleton<ISqlSugarClient>(s =>
            {
                SqlSugarScope sqlSugar = new SqlSugarScope(configConnection,
               db =>
               {
                   db.CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices()
                   {
                       //判断是否开启redis设置二级缓存方式
                       //DataInfoCacheService = new SqlSugarRedisCache(),
                       //模型定义为 int?号时自动为可空
                       EntityService = (c, p) =>
                       {
                           /***高版C#写法***/
                           //支持string?和string  
                           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) =>
                   //{
                   //    //获取作IOC作用域对象
                   //  //  var appServive = s.GetService<IHttpContextAccessor>();
                   //   // var obj = appServive?.HttpContext?.RequestServices.GetService<Log>();
                   // //   Console.WriteLine("AOP" + obj.GetHashCode());
                   //};
               });
                //sqlSugar.DbMaintenance.CreateDatabase();
                //Type[]? types = DbCoreUntil.DbCodeFirstModes("EzCoreNetFurion.Model", thisAttribute: typeof(CoderFirstAttribute));
 
 
                //sqlSugar.CodeFirst.InitTables(types ?? new Type[] { });
                return sqlSugar;
            });
 
 
        }
 
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
 
        }
    }
}