优化代码生成数据库字符串

This commit is contained in:
不做码农 2023-04-07 10:58:24 +08:00
parent 366889f360
commit 98af404a39
3 changed files with 23 additions and 16 deletions

View File

@ -14,6 +14,7 @@ namespace Infrastructure
public Upload Upload { get; set; }
public ALIYUN_OSS ALIYUN_OSS { get; set; }
public JwtSettings JwtSettings { get; set; }
public Gen Gen { get; set; }
}
/// <summary>
/// 发送邮件数据配置
@ -71,4 +72,13 @@ namespace Infrastructure
/// </summary>
public int Expire { get; set; } = 1440;
}
public class Gen
{
public string Conn { get; set; }
public int DbType { get; set; }
public string Database { get; set; }
}
}

View File

@ -7,8 +7,8 @@
}
},
"ConnectionStrings": {
"conn_db": "Data Source=LAPTOP-STKF2M8H\\SQLEXPRESS;User ID=sa;Password=zradmin123;Initial Catalog=ZrAdmin;Integrated Security=SSPI", //
"conn_db_type": "1" // MySql = 0, SqlServer = 1, Oracle = 3
"conn_db": "Data Source=LAPTOP-STKF2M8H\\SQLEXPRESS;User ID=admin;Password=admin123;Initial Catalog=ZrAdmin;", //
"conn_db_type": "1" // MySql = 0, SqlServer = 1, Oracle = 3PgSql = 4
},
"urls": "http://localhost:8888", //urldevServer
"corsUrls": "http://localhost:8887", //","
@ -43,7 +43,7 @@
"SendUser": "@all"
},
"gen": {
"conn": "Data Source=LAPTOP-STKF2M8H\\SQLEXPRESS;User ID=sa;Password=zradmin123;Initial Catalog=ZrAdmin;Integrated Security=SSPI",
"conn": "Data Source=LAPTOP-STKF2M8H\\SQLEXPRESS;User ID=admin;Password=admin123;Initial Catalog=ZrAdmin;",
"dbType": 1, //MySql = 0, SqlServer = 1
"autoPre": true, //
"author": "admin",

View File

@ -3,10 +3,7 @@ using Infrastructure.Extensions;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ZR.CodeGenerator
{
@ -24,27 +21,27 @@ namespace ZR.CodeGenerator
/// <returns></returns>
public SqlSugarClient GetSugarDbContext(string dbName = "")
{
string connStr = AppSettings.GetConfig(GenConstants.Gen_conn);
int dbType = AppSettings.GetAppConfig(GenConstants.Gen_conn_dbType, 0);
Gen options = new();
AppSettings.Bind("gen", options);
string connStr = options.Conn;
if (!string.IsNullOrEmpty(dbName))
{
string replaceStr = GetValue(connStr, "Database=", ";");
string replaceStr2 = GetValue(connStr, "Initial Catalog=", ";");
string replaceStr = GetValue(options.Conn, "Database=", ";");
string replaceStr2 = GetValue(options.Conn, "Initial Catalog=", ";");
if (replaceStr.IsNotEmpty())
{
connStr = connStr.Replace(replaceStr, dbName, StringComparison.OrdinalIgnoreCase);
connStr = options.Conn.Replace(replaceStr, dbName, StringComparison.OrdinalIgnoreCase);
}
if (replaceStr2.IsNotEmpty())
{
connStr = connStr.Replace(replaceStr2, dbName, StringComparison.OrdinalIgnoreCase);
connStr = options.Conn.Replace(replaceStr2, dbName, StringComparison.OrdinalIgnoreCase);
}
}
var db = new SqlSugarClient(new List<ConnectionConfig>()
{
new ConnectionConfig(){
ConnectionString = connStr,
DbType = (DbType)dbType,
DbType = (DbType)options.DbType,
IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样
InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
},
@ -63,7 +60,7 @@ namespace ZR.CodeGenerator
/// <returns></returns>
public static string GetValue(string str, string s, string e)
{
Regex rg = new Regex("(?<=(" + s + "))[.\\s\\S]*?(?=(" + e + "))", RegexOptions.Multiline | RegexOptions.Singleline);
Regex rg = new("(?<=(" + s + "))[.\\s\\S]*?(?=(" + e + "))", RegexOptions.Multiline | RegexOptions.Singleline);
return rg.Match(str).Value;
}
}