Base solution for your next web application
Open Closed

How to call methods of DbPerTenantConnectionStringResolver class ? #7956


User avatar
0
xitix created

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)
  • User Avatar
    0
    maliming created
    Support Team

    Can you explain this? This class will be called automatically because it implements IDbPerTenantConnectionStringResolver.

  • User Avatar
    0
    xitix created

    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.

  • User Avatar
    0
    ismcagdas created
    Support Team

    You can inject IDbPerTenantConnectionStringResolver and use it directly.

  • User Avatar
    0
    xitix created

    Sir I am sorry for my silliness. but I am unable to implemet it after injecting it. See below image. Kindly assist me , I think I am missing some minute things .

  • User Avatar
    0
    maliming created
    Support Team

    hi xitix

    Please see https://github.com/aspnetboilerplate/aspnetboilerplate/blob/e0ded5d8702f389aa1f5947d3446f16aec845287/src/Abp.ZeroCore.EntityFrameworkCore/Zero/EntityFrameworkCore/AbpZeroDbMigrator.cs#L66

  • User Avatar
    0
    ismcagdas created
    Support Team

    This issue is closed because it has not had recent activity for a long time.