Base solution for your next web application
Open Closed

How use multiple dynamic dbcontext at same time on different database.? #8078


User avatar
0
CNS created

i have two or more databases on different servers . server and database information saved in tenant table .. i want to change db context dynamically according to tenant at run time . i see this #4838 issue . but i can't understand how can i achieve this.


4 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    The abp framework will automatically use the corresponding connection string to create a DbContext based on the tenant information of the current context.

    You can look at this example, please share some of your code if you can. I want to see your use case. https://github.com/aspnetboilerplate/aspnetboilerplate-samples/tree/master/MultipleDbContextEfCoreDemo

  • User Avatar
    0
    CNS created

    i see the demo .. here is my code

    public class DbPerTenantConnectionStringResolver : DefaultConnectionStringResolver
        {
            private readonly ICurrentUnitOfWorkProvider _currentUnitOfWorkProvider;
            private readonly IConfigurationRoot _appConfiguration;
            private readonly IDBPerTenantAppService _IDBPerTenantAppService;
            public IAbpSession AbpSession { get; set; }
    
            public DbPerTenantConnectionStringResolver(ICurrentUnitOfWorkProvider currentUnitOfWorkProvider, IDBPerTenantAppService iDBPerTenantAppService, IAbpStartupConfiguration configuration, IHostingEnvironment hostingEnvironment)
                    : base(configuration)
            {
                _currentUnitOfWorkProvider = currentUnitOfWorkProvider;
                _IDBPerTenantAppService = iDBPerTenantAppService;
                AbpSession = NullAbpSession.Instance;
                _appConfiguration =
                        AppConfigurations.Get(hostingEnvironment.ContentRootPath, hostingEnvironment.EnvironmentName);
            }
    
            public override string GetNameOrConnectionString(ConnectionStringResolveArgs args)
            {
                if (args["DbContextConcreteType"] as Type == typeof(INVDbContext))
                {
    
                    var connctions = _IDBPerTenantAppService.GetAllConnections((int)GetCurrentTenantId(), "INV");
    
                    return connctions.Result.Items[0].VConnectionString; //_appConfiguration.GetConnectionString(ERPConsts.SecondDbConnectionStringName);
                }
    
                return base.GetNameOrConnectionString(args);
            }
    
            protected virtual int? GetCurrentTenantId()
            {
                return _currentUnitOfWorkProvider.Current != null
                    ? _currentUnitOfWorkProvider.Current.GetTenantId()
                    : AbpSession.TenantId;
            }
        }
    

    i have seperate databases for according to module (InventoryDb, PayrolDb, FinanceDb) and im getting the connectionstring from database table(abpConnection) . it works good. @maliming see the code for better suggestion.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @CNS

    Can we close this issue ? Is your problem solved ?

  • User Avatar
    0
    ismcagdas created
    Support Team

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