From 26d97687b18bf869a4029138b6786436588446c8 Mon Sep 17 00:00:00 2001 From: Nine Date: Tue, 11 Oct 2022 10:53:32 +0800 Subject: [PATCH] =?UTF-8?q?=20//=E4=BF=AE=E6=94=B9=E4=BD=BF=E7=94=A8=20mod?= =?UTF-8?q?el=E5=A4=87=E6=B3=A8=E5=AD=97=E6=AE=B5=E4=BD=9C=E4=B8=BA?= =?UTF-8?q?=E5=88=87=E6=8D=A2=E6=95=B0=E6=8D=AE=E5=BA=93=E6=9D=A1=E4=BB=B6?= =?UTF-8?q?=EF=BC=8C=E4=BD=BF=E7=94=A8sqlsugar=20TenantAttribute=E5=AD=98?= =?UTF-8?q?=E6=94=BE=E6=95=B0=E6=8D=AE=E5=BA=93ConnId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Blog.Core.Model/IDS4DbModels/ApplicationRole.cs | 3 ++- Blog.Core.Model/IDS4DbModels/ApplicationUser.cs | 3 ++- Blog.Core.Model/Models/PasswordLib.cs | 3 ++- Blog.Core.Repository/BASE/BaseRepository.cs | 14 +++++++++++--- 4 files changed, 17 insertions(+), 6 deletions(-) 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; } }