diff --git a/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs b/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs index 9e6051dd5..de5c91e46 100644 --- a/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs +++ b/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs @@ -87,7 +87,15 @@ namespace SqlSugar foreach (var item in properies) { var value = Activator.CreateInstance(item.PropertyType); - value.GetType().GetProperty("Context").SetValue(value, this); + TenantAttribute tenantAttribute = item.PropertyType.GetGenericArguments()[0].GetCustomAttribute(); + if (tenantAttribute == null) + { + value.GetType().GetProperty("Context").SetValue(value, this); + } + else + { + value.GetType().GetProperty("Context").SetValue(value, this.GetConnection(tenantAttribute.configId)); + } item.SetValue(result, value); } return result; diff --git a/Src/Asp.NetCore2/SqlSugar/SugarUnitOfWork.cs b/Src/Asp.NetCore2/SqlSugar/SugarUnitOfWork.cs index c14618f1a..4cefbdc2b 100644 --- a/Src/Asp.NetCore2/SqlSugar/SugarUnitOfWork.cs +++ b/Src/Asp.NetCore2/SqlSugar/SugarUnitOfWork.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; @@ -29,13 +30,30 @@ namespace SqlSugar public SimpleClient GetRepository() where T : class, new() { - return new SimpleClient(Db); + TenantAttribute tenantAttribute = typeof(T).GetCustomAttribute(); + if (tenantAttribute == null) + { + return new SimpleClient(Db); + } + else + { + return new SimpleClient(Db.AsTenant().GetConnection(tenantAttribute.configId)); + } } public RepositoryType GetMyRepository() where RepositoryType : ISugarRepository, new() { var result = new RepositoryType(); - result.Context = this.Db; + var type = typeof(RepositoryType).GetGenericArguments()[0]; + TenantAttribute tenantAttribute = type.GetCustomAttribute(); + if (tenantAttribute == null) + { + result.Context = this.Db; + } + else + { + result.Context = this.Db.AsTenant().GetConnection(tenantAttribute.configId); + } return result; }