From 3f8bf4a2be9cc0aeccfe7605233948de72d78ad9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 28 Jan 2021 14:19:00 +0300 Subject: [PATCH] Non Multi-Tenant entities should always use the host connection string. --- .../Volo/Abp/Domain/Entities/EntityHelper.cs | 13 +++++++-- .../EntityFrameworkCore/EfCoreRepository.cs | 29 +++++++++++++++++-- .../Repositories/MongoDB/MongoDbRepository.cs | 26 ++++++++++++++++- 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/EntityHelper.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/EntityHelper.cs index 5e81e2270d..2f8dd28afc 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/EntityHelper.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/EntityHelper.cs @@ -1,7 +1,5 @@ using System; -using System.Collections.Concurrent; using System.Collections.Generic; -using System.Linq; using System.Linq.Expressions; using System.Reflection; using JetBrains.Annotations; @@ -15,6 +13,17 @@ namespace Volo.Abp.Domain.Entities /// public static class EntityHelper { + public static bool IsMultiTenant() + where TEntity : IEntity + { + return IsMultiTenant(typeof(TEntity)); + } + + public static bool IsMultiTenant(Type type) + { + return typeof(IMultiTenant).IsAssignableFrom(type); + } + public static bool EntityEquals(IEntity entity1, IEntity entity2) { if (entity1 == null || entity2 == null) diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs index 4ba5457677..055c9c2de9 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs @@ -13,6 +13,7 @@ using Volo.Abp.Domain.Entities; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore.DependencyInjection; using Volo.Abp.Guids; +using Volo.Abp.MultiTenancy; namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore { @@ -21,18 +22,42 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore where TEntity : class, IEntity { [Obsolete("Use GetDbContextAsync() method.")] - protected virtual TDbContext DbContext => _dbContextProvider.GetDbContext(); + protected virtual TDbContext DbContext => GetDbContext(); [Obsolete("Use GetDbContextAsync() method.")] - DbContext IEfCoreRepository.DbContext => DbContext.As(); + DbContext IEfCoreRepository.DbContext => GetDbContext() as DbContext; async Task IEfCoreRepository.GetDbContextAsync() { return await GetDbContextAsync() as DbContext; } + [Obsolete("Use GetDbContextAsync() method.")] + private TDbContext GetDbContext() + { + // Multi-tenancy unaware entities should always use the host connection string + if (!EntityHelper.IsMultiTenant()) + { + using (CurrentTenant.Change(null)) + { + return _dbContextProvider.GetDbContext(); + } + } + + return _dbContextProvider.GetDbContext(); + } + protected virtual Task GetDbContextAsync() { + // Multi-tenancy unaware entities should always use the host connection string + if (!EntityHelper.IsMultiTenant()) + { + using (CurrentTenant.Change(null)) + { + return _dbContextProvider.GetDbContextAsync(); + } + } + return _dbContextProvider.GetDbContextAsync(); } diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs index b252c48999..c816abb86c 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs @@ -51,10 +51,34 @@ namespace Volo.Abp.Domain.Repositories.MongoDB } [Obsolete("Use GetDbContextAsync method.")] - protected virtual TMongoDbContext DbContext => DbContextProvider.GetDbContext(); + protected virtual TMongoDbContext DbContext => GetDbContext(); + + [Obsolete("Use GetDbContextAsync method.")] + private TMongoDbContext GetDbContext() + { + // Multi-tenancy unaware entities should always use the host connection string + if (!EntityHelper.IsMultiTenant()) + { + using (CurrentTenant.Change(null)) + { + return DbContextProvider.GetDbContext(); + } + } + + return DbContextProvider.GetDbContext(); + } protected Task GetDbContextAsync(CancellationToken cancellationToken = default) { + // Multi-tenancy unaware entities should always use the host connection string + if (!EntityHelper.IsMultiTenant()) + { + using (CurrentTenant.Change(null)) + { + return DbContextProvider.GetDbContextAsync(GetCancellationToken(cancellationToken)); + } + } + return DbContextProvider.GetDbContextAsync(GetCancellationToken(cancellationToken)); }