增加多租户的配置

This commit is contained in:
不做码农 2021-10-29 13:23:22 +08:00
parent 60c25735b0
commit 39fc3918fa
6 changed files with 28 additions and 34 deletions

View File

@ -51,7 +51,7 @@ namespace ZR.Admin.WebApi.Controllers
//TODO 搜索条件 //TODO 搜索条件
//predicate = predicate.And(m => m.Name.Contains(parm.Name)); //predicate = predicate.And(m => m.Name.Contains(parm.Name));
_GendemoService.Test();
var response = _GendemoService.GetPages(predicate.ToExpression(), parm); var response = _GendemoService.GetPages(predicate.ToExpression(), parm);
return SUCCESS(response); return SUCCESS(response);

View File

@ -164,6 +164,14 @@ namespace ZR.Admin.WebApi
Console.WriteLine($"[执行Sql出错]{e.Message}SQL={e.Sql}"); Console.WriteLine($"[执行Sql出错]{e.Message}SQL={e.Sql}");
Console.WriteLine(); Console.WriteLine();
}; };
//调式代码 用来打印SQL
DbScoped.SugarScope.GetConnection(1).Aop.OnLogExecuting = (sql, pars) =>
{
Console.BackgroundColor = ConsoleColor.Yellow;
Console.WriteLine("【SQL语句Bus】" + sql.ToLower() + "\r\n"
+ DbScoped.SugarScope.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
};
//Bus Db错误日志 //Bus Db错误日志
DbScoped.SugarScope.GetConnection(1).Aop.OnError = (e) => DbScoped.SugarScope.GetConnection(1).Aop.OnError = (e) =>
{ {

View File

@ -1,13 +1,9 @@
using Infrastructure; using Infrastructure.Model;
using Infrastructure.Model;
using SqlSugar; using SqlSugar;
using SqlSugar.IOC; using SqlSugar.IOC;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading.Tasks;
using ZR.Model; using ZR.Model;
namespace ZR.Repository namespace ZR.Repository
@ -19,34 +15,11 @@ namespace ZR.Repository
public class BaseRepository<T> : IBaseRepository<T> where T : class, new() public class BaseRepository<T> : IBaseRepository<T> where T : class, new()
{ {
public ISqlSugarClient Context; public ISqlSugarClient Context;
//private ISqlSugarClient _db public BaseRepository(string configId = "0") //: base(dbContext)
//{
// get
// {
// if (typeof(T).GetTypeInfo().GetCustomAttributes(typeof(SugarTable), true).FirstOrDefault((x => x.GetType() == typeof(SugarTable))) is SugarTable sugarTable && !string.IsNullOrEmpty(sugarTable.TableDescription))
// {
// Context.ChangeDatabase(sugarTable.TableDescription.ToLower());
// }
// else
// {
// Context.ChangeDatabase(0);
// }
// return Context;
// }
//}
public BaseRepository(ISqlSugarClient dbContext = null, string configId = "0") //: base(dbContext)
{
if (dbContext == null)
{ {
Context = DbScoped.SugarScope.GetConnection(configId);//根据类传入的ConfigId自动选择 Context = DbScoped.SugarScope.GetConnection(configId);//根据类传入的ConfigId自动选择
//_dbbase = Context;
} }
}
//public ISqlSugarClient Db
//{
// get { return _db; }
//}
#region add #region add
/// <summary> /// <summary>
/// 插入指定列使用 /// 插入指定列使用

View File

@ -2,6 +2,8 @@ using System;
using Infrastructure.Attribute; using Infrastructure.Attribute;
using ZR.Repository.System; using ZR.Repository.System;
using ZR.Model.Models; using ZR.Model.Models;
using SqlSugar;
using SqlSugar.IOC;
namespace ZR.Repository namespace ZR.Repository
{ {
@ -14,12 +16,17 @@ namespace ZR.Repository
[AppService(ServiceLifetime = LifeTime.Transient)] [AppService(ServiceLifetime = LifeTime.Transient)]
public class GendemoRepository : BaseRepository<Gendemo> public class GendemoRepository : BaseRepository<Gendemo>
{ {
public GendemoRepository() : base(configId: "1") private readonly ISqlSugarClient db;
public GendemoRepository()
{ {
db = DbScoped.SugarScope.GetConnection(1);
} }
#region #region
public void Test()
{
var date = db.GetDate();
}
#endregion #endregion
} }
} }

View File

@ -26,7 +26,12 @@ namespace ZR.Service.Business
{ {
_repository = repository; _repository = repository;
} }
#region #region
public void Test()
{
_repository.Test();
}
#endregion #endregion
} }
} }

View File

@ -11,5 +11,6 @@ namespace ZR.Service.Business
/// </summary> /// </summary>
public interface IGendemoService: IBaseService<Gendemo> public interface IGendemoService: IBaseService<Gendemo>
{ {
void Test();
} }
} }