diff --git a/Blog.Core.Model/IDS4DbModels/ApplicationRole.cs b/Blog.Core.Model/IDS4DbModels/ApplicationRole.cs index fd2c737..8baf76e 100644 --- a/Blog.Core.Model/IDS4DbModels/ApplicationRole.cs +++ b/Blog.Core.Model/IDS4DbModels/ApplicationRole.cs @@ -7,7 +7,8 @@ namespace Blog.Core.Model.IDS4DbModels /// 以下model 来自ids4项目,多库模式,为了调取ids4数据 /// 角色表 /// - [SugarTable("ApplicationRole", "WMBLOG_MYSQL_2")] + [SugarTable("ApplicationRole", "角色表")] //('数据库表名','数据库表备注') + [TenantAttribute("WMBLOG_MYSQL_2")] //('代表是哪个数据库,名字是appsettings.json 的 ConnId') public class ApplicationRole { public bool IsDeleted { get; set; } diff --git a/Blog.Core.Model/IDS4DbModels/ApplicationUser.cs b/Blog.Core.Model/IDS4DbModels/ApplicationUser.cs index f3d02ae..5052916 100644 --- a/Blog.Core.Model/IDS4DbModels/ApplicationUser.cs +++ b/Blog.Core.Model/IDS4DbModels/ApplicationUser.cs @@ -7,7 +7,8 @@ namespace Blog.Core.Model.IDS4DbModels /// 以下model 来自ids4项目,多库模式,为了调取ids4数据 /// 用户表 /// - [SugarTable("AspNetUsers", "WMBLOG_MYSQL_2")] + [SugarTable("AspNetUsers", "用户表")]//('数据库表名','数据库表备注') + [TenantAttribute("WMBLOG_MYSQL_2")] //('代表是哪个数据库,名字是appsettings.json 的 ConnId') public class ApplicationUser { public string LoginName { get; set; } diff --git a/Blog.Core.Model/Models/PasswordLib.cs b/Blog.Core.Model/Models/PasswordLib.cs index a5e458d..2b43c26 100644 --- a/Blog.Core.Model/Models/PasswordLib.cs +++ b/Blog.Core.Model/Models/PasswordLib.cs @@ -6,7 +6,8 @@ namespace Blog.Core.Model.Models /// /// 密码库表 /// - [SugarTable("PasswordLib", "WMBLOG_MSSQL_2")] + [SugarTable("PasswordLib", "密码库表")]//('数据库表名','数据库表备注') + [TenantAttribute("WMBLOG_MYSQL_2")] //('代表是哪个数据库,名字是appsettings.json 的 ConnId') public class PasswordLib { [SugarColumn(IsNullable = false, IsPrimaryKey = true, IsIdentity = true)] diff --git a/Blog.Core.Repository/BASE/BaseRepository.cs b/Blog.Core.Repository/BASE/BaseRepository.cs index 9ac7573..e25db9f 100644 --- a/Blog.Core.Repository/BASE/BaseRepository.cs +++ b/Blog.Core.Repository/BASE/BaseRepository.cs @@ -28,16 +28,24 @@ namespace Blog.Core.Repository.Base */ if (Appsettings.app(new string[] { "MutiDBEnabled" }).ObjToBool()) { - if (typeof(TEntity).GetTypeInfo().GetCustomAttributes(typeof(SugarTable), true).FirstOrDefault((x => x.GetType() == typeof(SugarTable))) is SugarTable sugarTable && !string.IsNullOrEmpty(sugarTable.TableDescription)) + //if (typeof(TEntity).GetTypeInfo().GetCustomAttributes(typeof(SugarTable), true).FirstOrDefault((x => x.GetType() == typeof(SugarTable))) is SugarTable sugarTable && !string.IsNullOrEmpty(sugarTable.TableDescription)) + //{ + // _dbBase.ChangeDatabase(sugarTable.TableDescription.ToLower()); + //} + //else + //{ + // _dbBase.ChangeDatabase(MainDb.CurrentDbConnId.ToLower()); + //} + //修改使用 model备注字段作为切换数据库条件,使用sqlsugar TenantAttribute存放数据库ConnId + if (typeof(TEntity).GetTypeInfo().GetCustomAttributes(typeof(TenantAttribute), true).FirstOrDefault((x => x.GetType() == typeof(TenantAttribute))) is TenantAttribute tenantAttribute) { - _dbBase.ChangeDatabase(sugarTable.TableDescription.ToLower()); + _dbBase.ChangeDatabase(tenantAttribute.configId.ToString().ToLower()); } else { _dbBase.ChangeDatabase(MainDb.CurrentDbConnId.ToLower()); } } - return _dbBase; } }