diff --git a/Infrastructure/OptionsSetting.cs b/Infrastructure/OptionsSetting.cs index c01060c..3c88ce3 100644 --- a/Infrastructure/OptionsSetting.cs +++ b/Infrastructure/OptionsSetting.cs @@ -6,13 +6,6 @@ namespace Infrastructure /// public class OptionsSetting { - public static string ConnAdmin = "conn_zrAdmin"; - public static string ConnDbType = "conn_zrAdmin_type"; - public static string ConnBus = "conn_bus"; - public static string ConnBusDbType = "conn_bus_type"; - - public string Redis { get; set; } - public string Database { get; set; } /// /// 是否演示模式 /// @@ -37,7 +30,6 @@ namespace Infrastructure /// public class Upload { - public string UploadDirectory { get; set; } public string UploadUrl { get; set; } } /// diff --git a/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs b/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs index 4b3ed0d..c2590d7 100644 --- a/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs +++ b/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs @@ -237,7 +237,7 @@ namespace ZR.Admin.WebApi.Controllers var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId); genTableInfo.Columns = GenTableColumnService.GenTableColumns(dto.TableId); - dto.DbType = AppSettings.GetAppConfig(OptionsSetting.ConnBusDbType, 0); + dto.DbType = AppSettings.GetAppConfig("gen:dbType", 0); dto.GenTable = genTableInfo; dto.IsPreview = true; //生成代码 @@ -263,7 +263,7 @@ namespace ZR.Admin.WebApi.Controllers var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId); genTableInfo.Columns = GenTableColumnService.GenTableColumns(dto.TableId); - dto.DbType = AppSettings.GetAppConfig(OptionsSetting.ConnBusDbType, 0); + dto.DbType = AppSettings.GetAppConfig("gen:dbType", 0); dto.GenTable = genTableInfo; //自定义路径 if (genTableInfo.GenType == "1") diff --git a/ZR.Admin.WebApi/Extensions/DbExtension.cs b/ZR.Admin.WebApi/Extensions/DbExtension.cs index 7228e83..4183a03 100644 --- a/ZR.Admin.WebApi/Extensions/DbExtension.cs +++ b/ZR.Admin.WebApi/Extensions/DbExtension.cs @@ -26,43 +26,43 @@ namespace ZR.Admin.WebApi.Extensions public static void AddDb(IConfiguration Configuration) { - string connStr = Configuration.GetConnectionString(OptionsSetting.ConnAdmin); - string connStrBus = Configuration.GetConnectionString(OptionsSetting.ConnBus); - - int dbType = Convert.ToInt32(Configuration[OptionsSetting.ConnDbType]); - int dbType_bus = Convert.ToInt32(Configuration[OptionsSetting.ConnBusDbType]); + string connStr = Configuration.GetConnectionString("conn_db"); + int dbType = Convert.ToInt32(Configuration["conn_db_dbtype"]); SugarIocServices.AddSqlSugar(new List() { new IocConfig() { - ConfigId = "0", + ConfigId = "0",//默认db ConnectionString = connStr, DbType = (IocDbType)dbType, IsAutoCloseConnection = true - }, new IocConfig() { + }, + new IocConfig() { ConfigId = "1", - ConnectionString = connStrBus, - DbType = (IocDbType)dbType_bus, + ConnectionString = "替换成你的字符串", + DbType = IocDbType.MySql, IsAutoCloseConnection = true } + //...增加其他数据库 }); SugarIocServices.ConfigurationSugar(db => { + //db0数据过滤 FilterData(0); - //FilterData(1); + #region db0 - db.GetConnection(0).Aop.OnLogExecuting = (sql, pars) => + db.GetConnectionScope(0).Aop.OnLogExecuting = (sql, pars) => { - var param = db.GetConnection(0).Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)); + var param = db.GetConnectionScope(0).Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)); logger.Info($"【sql语句】{sql},{param}\n"); }; - db.GetConnection(0).Aop.OnError = (e) => + db.GetConnectionScope(0).Aop.OnError = (e) => { logger.Error(e, $"执行SQL出错:{e.Message}"); }; //SQL执行完 - db.GetConnection(0).Aop.OnLogExecuted = (sql, pars) => + db.GetConnectionScope(0).Aop.OnLogExecuted = (sql, pars) => { //执行完了可以输出SQL执行时间 (OnLogExecutedDelegate) }; @@ -86,7 +86,7 @@ namespace ZR.Admin.WebApi.Extensions } /// - /// 分页获取count 不会追加sql + /// 数据过滤 /// /// 多库id private static void FilterData(int configId) @@ -98,7 +98,7 @@ namespace ZR.Admin.WebApi.Extensions if (user == null) return; //管理员不过滤 if (user.RoleIds.Any(f => f.Equals("admin"))) return; - var db = DbScoped.SugarScope.GetConnection(configId); + var db = DbScoped.SugarScope.GetConnectionScope(configId); foreach (var role in user.Roles.OrderBy(f => f.DataScope)) { string dataScope = role.DataScope; diff --git a/ZR.Admin.WebApi/appsettings.json b/ZR.Admin.WebApi/appsettings.json index 0421a75..d9f765b 100644 --- a/ZR.Admin.WebApi/appsettings.json +++ b/ZR.Admin.WebApi/appsettings.json @@ -7,21 +7,18 @@ } }, "ConnectionStrings": { - "conn_zrAdmin": "server=LAPTOP-STKF2M8H\\SQLEXPRESS;uid=sa;pwd=zradmin123;database=ZrAdmin;Trusted_Connection=SSPI", - "conn_bus": "server=LAPTOP-STKF2M8H\\SQLEXPRESS;uid=zr;pwd=zradmin123;database=ZrAdmin;Trusted_Connection=SSPI" + "conn_db": "server=LAPTOP-STKF2M8H\\SQLEXPRESS;uid=sa;pwd=zradmin123;database=ZrAdmin;Trusted_Connection=SSPI" }, - "conn_zrAdmin_type": 1, //MySql = 0, SqlServer = 1 - "conn_bus_type": 1, + "conn_db_dbtype": 1, //ݿ MySql = 0, SqlServer = 1 "urls": "http://localhost:8888", //ĿurlĶ˿ǰ˶ӦdevServerҲҪ޸ "corsUrls": "http://localhost:8887", //ַǰĿǰ˷뵥Ҫã"," "JwtSettings": { "Issuer": "ZRAdmin.NET", "Audience": "ZRAdmin.NET", "SecretKey": "SecretKey-ZRADMIN.NET-20210101", - "Expire": 30//jwt¼ʱ䣨֣ + "Expire": 1440 //jwt¼ʱ䣨֣ }, "DemoMode": false, //Ƿʾģʽ - "DbKey": "", //ݿkey "Upload": { "UploadUrl": "http://localhost:8888" }, diff --git a/ZR.Repository/BaseRepository.cs b/ZR.Repository/BaseRepository.cs index c10ef49..16abe27 100644 --- a/ZR.Repository/BaseRepository.cs +++ b/ZR.Repository/BaseRepository.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Data; using System.Linq.Expressions; +using System.Reflection; using ZR.Model; namespace ZR.Repository @@ -18,22 +19,17 @@ namespace ZR.Repository public BaseRepository(ISqlSugarClient context = null) : base(context) { //通过特性拿到ConfigId - //var configId = typeof(T).GetCustomAttribute()?.configId; - //if (configId != null) - //{ - // itenant = DbScoped.SugarScope;//设置租户接口 - // Context = DbScoped.SugarScope.GetConnection(configId); - //} - //else - //{ - // Context = context ?? DbScoped.SugarScope.GetConnection(1);//根据类传入的ConfigId自动选择 - //} - Context = DbScoped.SugarScope.GetConnectionWithAttr(); - itenant = DbScoped.SugarScope;//设置租户接口 - if (Context == null) + var configId = typeof(T).GetCustomAttribute()?.configId; + if (configId != null) { - Context = DbScoped.SugarScope.GetConnection(1);//根据类传入的ConfigId自动选择 + Context = DbScoped.SugarScope.GetConnectionScope(configId);//根据类传入的ConfigId自动选择 } + else + { + Context = context ?? DbScoped.SugarScope.GetConnectionScope(0);//没有默认db0 + } + //Context = DbScoped.SugarScope.GetConnectionScopeWithAttr(); + itenant = DbScoped.SugarScope;//设置租户接口 } #region add