Browse Source

Non Multi-Tenant entities should always use the host connection string.

pull/7509/head
Halil İbrahim Kalkan 6 years ago
parent
commit
3f8bf4a2be
  1. 13
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/EntityHelper.cs
  2. 29
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs
  3. 26
      framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs

13
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
/// </summary>
public static class EntityHelper
{
public static bool IsMultiTenant<TEntity>()
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)

29
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<TEntity>.DbContext => DbContext.As<DbContext>();
DbContext IEfCoreRepository<TEntity>.DbContext => GetDbContext() as DbContext;
async Task<DbContext> IEfCoreRepository<TEntity>.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<TEntity>())
{
using (CurrentTenant.Change(null))
{
return _dbContextProvider.GetDbContext();
}
}
return _dbContextProvider.GetDbContext();
}
protected virtual Task<TDbContext> GetDbContextAsync()
{
// Multi-tenancy unaware entities should always use the host connection string
if (!EntityHelper.IsMultiTenant<TEntity>())
{
using (CurrentTenant.Change(null))
{
return _dbContextProvider.GetDbContextAsync();
}
}
return _dbContextProvider.GetDbContextAsync();
}

26
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<TEntity>())
{
using (CurrentTenant.Change(null))
{
return DbContextProvider.GetDbContext();
}
}
return DbContextProvider.GetDbContext();
}
protected Task<TMongoDbContext> GetDbContextAsync(CancellationToken cancellationToken = default)
{
// Multi-tenancy unaware entities should always use the host connection string
if (!EntityHelper.IsMultiTenant<TEntity>())
{
using (CurrentTenant.Change(null))
{
return DbContextProvider.GetDbContextAsync(GetCancellationToken(cancellationToken));
}
}
return DbContextProvider.GetDbContextAsync(GetCancellationToken(cancellationToken));
}

Loading…
Cancel
Save