⚡ 优化代码生成数据库连接
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using Infrastructure;
|
||||
using Infrastructure.Extensions;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -21,27 +20,22 @@ namespace ZR.CodeGenerator
|
||||
/// <returns></returns>
|
||||
public SqlSugarClient GetSugarDbContext(string dbName = "")
|
||||
{
|
||||
Gen options = new();
|
||||
AppSettings.Bind("gen", options);
|
||||
string connStr = options.Conn;
|
||||
List<DbConfigs> dbConfigs = AppSettings.Get<List<DbConfigs>>("dbConfigs");
|
||||
|
||||
DbConfigs configs = dbConfigs.Find(f => f.IsGenerateDb == true);
|
||||
string connStr = configs.Conn;
|
||||
|
||||
if (!string.IsNullOrEmpty(dbName))
|
||||
{
|
||||
string replaceStr = GetValue(options.Conn, "Database=", ";");
|
||||
string replaceStr2 = GetValue(options.Conn, "Initial Catalog=", ";");
|
||||
if (replaceStr.IsNotEmpty())
|
||||
{
|
||||
connStr = options.Conn.Replace(replaceStr, dbName, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
if (replaceStr2.IsNotEmpty())
|
||||
{
|
||||
connStr = options.Conn.Replace(replaceStr2, dbName, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
configs.DbName = dbName;
|
||||
}
|
||||
connStr = connStr.Replace("{dbName}", configs.DbName, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
var db = new SqlSugarClient(new List<ConnectionConfig>()
|
||||
{
|
||||
new ConnectionConfig(){
|
||||
ConnectionString = connStr,
|
||||
DbType = (DbType)options.DbType,
|
||||
DbType = (DbType)configs.DbType,
|
||||
IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样
|
||||
InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
|
||||
},
|
||||
|
||||
@@ -7,12 +7,10 @@ namespace ZR.CodeGenerator
|
||||
/// </summary>
|
||||
public class GenConstants
|
||||
{
|
||||
public static string Gen_conn = "gen:conn";
|
||||
public static string Gen_conn_dbType = "gen:dbType";
|
||||
public static string Gen_author = "gen:author";
|
||||
public static string Gen_autoPre = "gen:autoPre";
|
||||
public static string Gen_tablePrefix = "gen:tablePrefix";
|
||||
public static string Gen_oracle_db = "gen:oracle_db";
|
||||
|
||||
/// <summary>
|
||||
/// InputDto输入实体是不包含字段
|
||||
|
||||
@@ -16,15 +16,15 @@ namespace ZR.CodeGenerator.Service
|
||||
{
|
||||
var db = GetSugarDbContext();
|
||||
//Oracle库特殊处理
|
||||
var dbType = AppSettings.GetAppConfig(GenConstants.Gen_conn_dbType, 0);
|
||||
if (dbType == 3)
|
||||
List<DbConfigs> dbConfigs = AppSettings.Get<List<DbConfigs>>("dbConfigs");
|
||||
DbConfigs configs = dbConfigs.Find(f => f.IsGenerateDb == true);
|
||||
if (configs.DbType == 3)
|
||||
{
|
||||
var defaultDb = AppSettings.GetAppConfig(GenConstants.Gen_oracle_db, string.Empty);
|
||||
return new List<string>() { defaultDb };
|
||||
return new List<string>() { configs?.DbName };
|
||||
}
|
||||
var templist = db.DbMaintenance.GetDataBaseList(db);
|
||||
|
||||
return templist;
|
||||
return templist.FindAll(f => !f.Contains("schema"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user