优化代码生成数据库字符串
This commit is contained in:
parent
366889f360
commit
98af404a39
@ -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; }
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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 = 3,PgSql = 4
|
||||
},
|
||||
"urls": "http://localhost:8888", //项目启动url,如果改动端口前端对应devServer也需要进行修改
|
||||
"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",
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user