Kindly Guide me to call this method GetNameOrConnectionString() for multitenancy.
public class DbPerTenantConnectionStringResolver : DefaultConnectionStringResolver, IDbPerTenantConnectionStringResolver
{
/// <summary>
/// Reference to the session.
/// </summary>
public IAbpSession AbpSession { get; set; }
private readonly ICurrentUnitOfWorkProvider _currentUnitOfWorkProvider;
private readonly ITenantCache _tenantCache;
/// <summary>
/// Initializes a new instance of the <see cref="DbPerTenantConnectionStringResolver"/> class.
/// </summary>
public DbPerTenantConnectionStringResolver(
IAbpStartupConfiguration configuration,
ICurrentUnitOfWorkProvider currentUnitOfWorkProvider,
ITenantCache tenantCache)
: base(configuration)
{
_currentUnitOfWorkProvider = currentUnitOfWorkProvider;
_tenantCache = tenantCache;
AbpSession = NullAbpSession.Instance;
}
public override string GetNameOrConnectionString(ConnectionStringResolveArgs args)
{
if (args.MultiTenancySide == MultiTenancySides.Host)
{
return GetNameOrConnectionString(new DbPerTenantConnectionStringResolveArgs(null, args));
}
return GetNameOrConnectionString(new DbPerTenantConnectionStringResolveArgs(GetCurrentTenantId(), args));
}
public virtual string GetNameOrConnectionString(DbPerTenantConnectionStringResolveArgs args)
{
if (args.TenantId == null)
{
//Requested for host
return base.GetNameOrConnectionString(args);
}
var tenantCacheItem = _tenantCache.Get(args.TenantId.Value);
if (tenantCacheItem.ConnectionString.IsNullOrEmpty())
{
//Tenant has not dedicated database
return base.GetNameOrConnectionString(args);
}
return tenantCacheItem.ConnectionString;
}
protected virtual int? GetCurrentTenantId()
{
return _currentUnitOfWorkProvider.Current != null
? _currentUnitOfWorkProvider.Current.GetTenantId()
: AbpSession.TenantId;
}
}
6 Answer(s)
-
0
Can you explain this? This class will be called automatically because it implements IDbPerTenantConnectionStringResolver.
-
0
Can you explain this? Actually I have repository class where am calling database SPs. so I want to call IDbPerTenantConnectionStringResolver Class from repository class in EnityFramework.
Or Do I have to inherit this class to my repository class ?
Basically I don't the way to make it fully multitenant. So many thing i am gussing trying.
-
0
You can inject
IDbPerTenantConnectionStringResolver
and use it directly. -
0
-
0
hi xitix
Please see https://github.com/aspnetboilerplate/aspnetboilerplate/blob/e0ded5d8702f389aa1f5947d3446f16aec845287/src/Abp.ZeroCore.EntityFrameworkCore/Zero/EntityFrameworkCore/AbpZeroDbMigrator.cs#L66
-
0
This issue is closed because it has not had recent activity for a long time.