username@email.com
2024-10-29 18a8fab394f764e5b30c48c8e0d6887ef7d44cbf
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
87
88
89
90
91
92
93
94
95
96
97
98
99
/***********************************************************************
 *            Project: baifenBinfa.Net                                     *
 *                Web: https://baifenBinfa.com                             *
 *        ProjectName: 百分兵法管理系统                               *
 *             Author:                                        *
 *              Email:                               *
 *           Versions: 1.0                                             *
 *         CreateTime: 2020-02-03 22:45:34
 *           FileName: SqlSugarSetup
 *   ClassDescription: 
 ***********************************************************************/
 
 
using System;
using System.Collections.Generic;
using CoreCms.Net.Caching.SqlSugar;
using CoreCms.Net.Configuration;
using CoreCms.Net.Loging;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using SqlSugar.IOC;
 
namespace CoreCms.Net.Core.Config
{
    /// <summary>
    /// SqlSugar 启动服务
    /// </summary>
    public static class SqlSugarSetup
    {
 
        public static void AddSqlSugarSetup(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));
 
            //注入 ORM
            //SugarIocServices.AddSqlSugar(new IocConfig()
            //{
            //    //数据库连接
            //    ConnectionString = AppSettingsConstVars.DbSqlConnection,
            //    //判断数据库类型
            //    DbType = AppSettingsConstVars.DbDbType == IocDbType.MySql.ToString() ? IocDbType.MySql : IocDbType.SqlServer,
            //    //是否开启自动关闭数据库连接
            //    IsAutoCloseConnection = true
            //});
            SugarIocServices.AddSqlSugar(new List<IocConfig>
            {
               new   IocConfig{//数据库连接
                ConnectionString = AppSettingsConstVars.DbSqlConnection,
                //判断数据库类型
                DbType = AppSettingsConstVars.DbDbType == IocDbType.MySql.ToString() ? IocDbType.MySql : IocDbType.SqlServer,
                //是否开启自动关闭数据库连接
                IsAutoCloseConnection = true
                },
               new IocConfig
               {
                    ConfigId=AppSettingsConstVars.CYDbDbID,
                    ConnectionString=AppSettingsConstVars.CyDbSqlConnection,
                       DbType = AppSettingsConstVars.CyDbDbType == IocDbType.MySql.ToString() ? IocDbType.MySql : IocDbType.SqlServer,
                       //是否开启自动关闭数据库连接
                IsAutoCloseConnection = true
               }
 
            });
            //设置参数
            services.ConfigurationSugar(db =>
            {
                db.CurrentConnectionConfig.InitKeyType = InitKeyType.Attribute;
                db.CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices()
                {
                    //判断是否开启redis设置二级缓存方式
                    DataInfoCacheService = AppSettingsConstVars.RedisUseCache ? new SqlSugarRedisCache() : new SqlSugarMemoryCache()
                };
                db.CurrentConnectionConfig.MoreSettings = new ConnMoreSettings()
                {
                    //所有 增、删 、改 会自动调用.RemoveDataCache()清理二级缓存
                    IsAutoRemoveDataCache = true
                };
 
                //执行SQL 错误事件,可监控sql(暂时屏蔽,需要可开启)
                //db.Aop.OnLogExecuting = (sql, p) =>
                //{
                //    NLogUtil.WriteFileLog(NLog.LogLevel.Error, LogType.Other, "SqlSugar执行SQL错误事件打印Sql", sql);
                //};
 
                //执行SQL 错误事件
                db.Aop.OnError = (exp) =>
                {
                    NLogUtil.WriteFileLog(NLog.LogLevel.Error, LogType.Other, "SqlSugar", "执行SQL错误事件", exp);
                };
 
                //设置更多连接参数
                //db.CurrentConnectionConfig.XXXX=XXXX
                //db.CurrentConnectionConfig.MoreSetting=new MoreSetting(){}
                //读写分离等都在这儿设置
            });
 
        }
    }
}