Skip to content
.NET 开发者指北.NET 开发者指北
CMS
.NET指北
FreeKit
Docker
关于
博客
github icon
    • Lin CMS By .NET
      • 后端准备
        • 后端快速上手
          • 相关技术
            • 前端准备
              • 开源介绍
                • 功能模块的设计
                  • 产品设计
                    • GitHub Actions
                      • LinCms如何切换成SQL server
                        • 数据库配置
                          • Serilog配置
                            • CAP配置

                          LinCms如何切换成SQL server

                          calendar icon2021年12月22日timer icon大约 2 分钟word icon约 710 字

                          此页内容
                          • 数据库配置
                          • Serilog配置
                          • CAP配置

                          # LinCms如何切换成SQL server

                          需要修改LinCms.Web中的appsetting.json配置

                          # 数据库配置

                          DefaultDB:改成1 SqlServer:改成自己的数据库配置

                            "ConnectionStrings": {
                          -    "DefaultDB": "0",
                          +    "DefaultDB": "1",
                              "DataType": {
                                "MySql": 0,
                                "SqlServer": 1,
                                "PostgreSQL": 2,
                                "Oracle": 3,
                                "Sqlite": 4
                              },
                              "MySql": "Data Source=localhost;Port=3306;User ID=root;Password=rCsRedisoot;Initial Catalog=lincms;Charset=utf8mb4;SslMode=none;Max pool size=1;Connection LifeTime=20"
                          +   "SqlServer": "Data Source=.;User ID=sa;Password=123456;Integrated Security=True;Initial Catalog=LinCMS;Pooling=true;Min Pool Size=1"
                            },
                          
                          1
                          2
                          3
                          4
                          5
                          6
                          7
                          8
                          9
                          10
                          11
                          12
                          13

                          # Serilog配置

                          Using引用 "Serilog.Sinks.MSSqlServer",去掉 "Serilog.Sinks.MariaDB"

                          把 "Name"为 "MariaDB"的json注释掉。

                          把"Name"为 "MSSqlServer"的json注释去掉。

                          并改掉Args:connectionString的连接串。

                              "Serilog": {
                              "Using": [
                                "Serilog.Sinks.Console",
                                "Serilog.Sinks.File",
                          -      "Serilog.Sinks.MariaDB"
                          +      "Serilog.Sinks.MSSqlServer"
                              ],
                              "MinimumLevel": {
                                "Default": "Information",
                                "Override": {
                                  "Microsoft": "Information",
                                  "System": "Warning"
                                }
                              },
                              "WriteTo": [
                                {
                                  "Name": "File",
                                  "Args": {
                                    "path": "Logs/log.txt",
                                    "rollingInterval": "Day"
                                  }
                                },
                                {
                                  "Name": "Console",
                                  "Args": {
                                    "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
                                    "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}"
                                  }
                                },
                          -      {
                                  "Name": "MariaDB",
                                  "Args": {
                                    "connectionString": "Data Source=localhost;Port=3308;User ID=root;Password=root;Initial Catalog=lincms;Charset=utf8mb4;SslMode=none;Max pool size=1;Connection LifeTime=20",
                                    "autoCreateTable": true,
                                    "tableName": "app_serilog",
                                    "restrictedToMinimumLevel": "Information",
                                    "batchPostingLimit": 4, //1000
                                    "period": "0.00:00:03",
                                    "options": {
                                      "PropertiesToColumnsMapping": {
                                        "Exception": "exception",
                                        "Level": "level",
                                        "Message": "message",
                                        "MessageTemplate": "message_template",
                                        "Properties": "properties",
                                        "Timestamp": "timestamp"
                                      },
                                      "TimestampInUtc": false,
                                      "ExcludePropertiesWithDedicatedColumn": true,
                                      "EnumsAsInts": true,
                                      "LogRecordsCleanupFrequency": "0.02:00:00",
                                      "LogRecordsExpiration": "31.00:00:00"
                                    }
                                  }
                          -      }
                          +      //{
                                //  "Name": "MSSqlServer",
                                //  "Args": {
                                //    "connectionString": "Data Source=.;User ID=sa;Password=123456;Integrated Security=True;Initial Catalog=LinCMS;Pooling=true;Min Pool Size=1",
                                //    "restrictedToMinimumLevel": "Information",
                                //    "sinkOptionsSection": {
                                //      "tableName": "app_serilog",
                                //      "schemaName": "dbo",
                                //      "autoCreateSqlTable": true,
                                //      "batchPostingLimit": 1000,
                                //      "period": "0.00:00:15"
                                //    },
                                //    "columnOptionsSection": {
                                //      "disableTriggers": true,
                                //      "clusteredColumnstoreIndex": false,
                                //      "primaryKeyColumnName": "id",
                                //      "addStandardColumns": [ "LogEvent" ],
                                //      "removeStandardColumns": [ "Properties" ],
                                //      "additionalColumns": [
                                //        {
                                //          "ColumnName": "event_type",
                                //          "DataType": "int",
                                //          "AllowNull": true
                                //        },
                                //        {
                                //          "ColumnName": "release",
                                //          "DataType": "varchar",
                                //          "DataLength": 32
                                //        },
                                //        {
                                //          "ColumnName": "environment_user_name",
                                //          "PropertyName": "UserName",
                                //          "DataType": "varchar",
                                //          "DataLength": 50
                                //        },
                                //        {
                                //          "ColumnName": "all_sqlcolumn_defaults",
                                //          "DataType": "varchar",
                                //          "AllowNull": true,
                                //          "DataLength": -1,
                                //          "NonClusteredIndex": false
                                //        }
                                //      ],
                                //      "id": {
                                //        "columnName": "id",
                                //        "nonClusteredIndex": true
                                //      },
                                //      "level": {
                                //        "columnName": "level",
                                //        "storeAsEnum": true
                                //      },
                                //      //"Properties": {
                                //      //  "columnName": "properties",
                                //      //  "excludeAdditionalProperties": true,
                                //      //  "dictionaryElementName": "dict",
                                //      //  "itemElementName": "item",
                                //      //  "omitDictionaryContainerElement": false,
                                //      //  "omitSequenceContainerElement": false,
                                //      //  "omitStructureContainerElement": false,
                                //      //  "omitElementIfEmpty": true,
                                //      //  "propertyElementName": "prop",
                                //      //  "rootElementName": "root",
                                //      //  "sequenceElementName": "seq",
                                //      //  "structureElementName": "struct",
                                //      //  "usePropertyKeyAsElementName": false
                                //      //},
                                //      "TimeStamp": {
                                //        "columnName": "timestamp",
                                //        "convertToUtc": true,
                                //        "DataType": "tinyint"
                                //      },
                                //      "MessageTemplate": {
                                //        "columnName": "message_template",
                                //        "convertToUtc": true
                                //      },
                                //      "Timestamp": "timestamp",
                                //      "Exception": {
                                //        "columnName": "exception"
                                //      },
                                //      "Message": {
                                //        "columnName": "message"
                                //      },
                                //      "logEvent": {
                                //        "columnName": "properties",
                                //        "excludeAdditionalProperties": true,
                                //        "excludeStandardColumns": true
                                //      }
                                //    }
                                //  }
                          +      //}
                              ]
                            }
                          
                          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
                          100
                          101
                          102
                          103
                          104
                          105
                          106
                          107
                          108
                          109
                          110
                          111
                          112
                          113
                          114
                          115
                          116
                          117
                          118
                          119
                          120
                          121
                          122
                          123
                          124
                          125
                          126
                          127
                          128
                          129
                          130
                          131
                          132
                          133
                          134
                          135
                          136
                          137
                          138
                          139
                          140
                          141
                          142
                          143
                          144
                          145
                          146
                          147

                          # CAP配置

                          因为用了CAP。cap换sql server。这里改成 DefaultStorage改成2。

                          如果Messqueue换rabbitmq的话,DefaultMessageQueue就改成1.然后配置 RabbitMQ

                            "CAP": {
                          -    "DefaultStorage": "1",
                          +    "DefaultStorage": "2",
                              "DefaultMessageQueue": "0",
                              "Storage": {
                                "InMemoryStorage": 0,
                                "MySql": 1,
                                "SqlServer": 2
                              },
                              "MessageQueue": {
                                "InMemoryQueue": 0,
                                "RabbitMQ": 1
                              },
                              "RabbitMQ": {
                                "HostName": "localhost",
                                "UserName": "admin",
                                "Password": "123456",
                                "Port": 5672,
                                "VirtualHost": "/admin"
                              }
                            },
                          
                          1
                          2
                          3
                          4
                          5
                          6
                          7
                          8
                          9
                          10
                          11
                          12
                          13
                          14
                          15
                          16
                          17
                          18
                          19
                          20
                          21

                          双击LinCms.Application项目,注释掉DotNetCore.CAP.MySql的引用,引用 SqlServer的包

                          	<ItemGroup>
                          		<PackageReference Include="AspNet.Security.OAuth.Gitee" Version="5.0.11" />
                          		<PackageReference Include="AspNet.Security.OAuth.GitHub" Version="5.0.11" />
                          		<PackageReference Include="AspNet.Security.OAuth.QQ" Version="5.0.11" />
                          		<PackageReference Include="AutoMapper" Version="10.1.1" />
                          		<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.1.0" />
                          -		<PackageReference Include="DotNetCore.CAP.MySql" Version="5.1.3" />
                          +		<PackageReference Include="DotNetCore.CAP.SqlServer" Version="5.1.3" />
                          		<PackageReference Include="IdentityServer4" Version="4.1.2" />
                          		<PackageReference Include="DotNetCore.CAP" Version="5.1.3" />
                          		<PackageReference Include="IdentityModel" Version="5.1.0" />
                          		<PackageReference Include="MQiniu.Core" Version="1.0.1" />
                          		<PackageReference Include="Caching.CSRedis" Version="3.6.60" />
                          		<PackageReference Include="CSRedisCore" Version="3.6.6" />
                          	</ItemGroup>
                          
                          
                          1
                          2
                          3
                          4
                          5
                          6
                          7
                          8
                          9
                          10
                          11
                          12
                          13
                          14
                          15
                          16

                          打开LinCms.Application项目中的CapUnitOfWorkExtensions.cs文件

                          修改方法BeginTransaction

                          public static ICapTransaction BeginTransaction(this IUnitOfWork unitOfWork, ICapPublisher publisher, bool autoCommit = false)
                          {
                              //看了源码,换了新的写法,换不同的数据库,就需要手动修改这段代码了(MySqlCapTransaction)
                              //publisher.Transaction.Value = (ICapTransaction)publisher.ServiceProvider.GetService(typeof(ICapTransaction));新版本只能得到nul
                          -   publisher.Transaction.Value = ActivatorUtilities.CreateInstance<MySqlCapTransaction>(publisher.ServiceProvider);
                          +   publisher.Transaction.Value = ActivatorUtilities.CreateInstance<SqlServerCapTransaction>(publisher.ServiceProvider);
                          }
                          
                          1
                          2
                          3
                          4
                          5
                          6
                          7
                          edit icon在 GitHub 上编辑此页open in new window
                          上次编辑于: 2021/12/22 18:31:25
                          贡献者: luoyunchong
                          上一页
                          GitHub Actions
                          MIT Licensed | Copyright © 2021-present luoyunchong
                          苏ICP备16046457号-1

                          该应用可以安装在你的 PC 或移动设备上。这将使该 Web 应用程序外观和行为与其他应用程序相同。它将在出现在应用程序列表中,并可以固定到主屏幕,开始菜单或任务栏。此 Web 应用程序还将能够与其他应用程序和你的操作系统安全地进行交互。

                          详情