//修改使用 model备注字段作为切换数据库条件,使用sqlsugar TenantAttribute存放数据库ConnId

This commit is contained in:
Nine 2022-10-11 10:53:32 +08:00
parent 2fbd8ac856
commit 26d97687b1
4 changed files with 17 additions and 6 deletions

View File

@ -7,7 +7,8 @@ namespace Blog.Core.Model.IDS4DbModels
/// 以下model 来自ids4项目多库模式为了调取ids4数据
/// 角色表
/// </summary>
[SugarTable("ApplicationRole", "WMBLOG_MYSQL_2")]
[SugarTable("ApplicationRole", "角色表")] //('数据库表名''数据库表备注')
[TenantAttribute("WMBLOG_MYSQL_2")] //('代表是哪个数据库名字是appsettings.json 的 ConnId')
public class ApplicationRole
{
public bool IsDeleted { get; set; }

View File

@ -7,7 +7,8 @@ namespace Blog.Core.Model.IDS4DbModels
/// 以下model 来自ids4项目多库模式为了调取ids4数据
/// 用户表
/// </summary>
[SugarTable("AspNetUsers", "WMBLOG_MYSQL_2")]
[SugarTable("AspNetUsers", "用户表")]//('数据库表名''数据库表备注')
[TenantAttribute("WMBLOG_MYSQL_2")] //('代表是哪个数据库名字是appsettings.json 的 ConnId')
public class ApplicationUser
{
public string LoginName { get; set; }

View File

@ -6,7 +6,8 @@ namespace Blog.Core.Model.Models
/// <summary>
/// 密码库表
/// </summary>
[SugarTable("PasswordLib", "WMBLOG_MSSQL_2")]
[SugarTable("PasswordLib", "密码库表")]//('数据库表名''数据库表备注')
[TenantAttribute("WMBLOG_MYSQL_2")] //('代表是哪个数据库名字是appsettings.json 的 ConnId')
public class PasswordLib
{
[SugarColumn(IsNullable = false, IsPrimaryKey = true, IsIdentity = true)]

View File

@ -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;
}
}