Base solution for your next web application
Open Closed

Multiple database based on tenant #748


User avatar
0
jatin created

Hello hikalkan,

I am using Asp.Net Zero framework.

I want multiple database based on current tenant.

So I have one idea like use parameterize constructor of MCDbContext and once we create object at that time we can pass connection string name or database name.

For that web.config need to be update to date with all databases connection strings. We agree for same

But in coding architecture i have seen connection string name "Default" in MCDataModule class. So please give me advise how can i change connection string name on run time.

If connection string are using from MCDbContext then definitely my idea will work. But in Asp.Net Zero framework getting connection string name from MCDataModule :(

Awaiting for your response.

Thanks, Jatin


4 Answer(s)
  • User Avatar
    0
    jatin created

    Hello,

    Any update for me?

    Still waiting for anyone response on my query.

    Thanks, Jatin

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    Connection string is used which is in the dbcontext. So you can change the constructor. It's something like that:

        public AbpProjectNameDbContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {
    
        }
    

    You can change it like that:

        public AbpProjectNameDbContext(string nameOrConnectionString)
            : base(GetConnString())
        {
    
        }
    

    You should create this GetConnString() and return the connection string you want.

  • User Avatar
    0
    jatin created

    Hello Hikalkan,

    Thank you for your answer.

    You are right and I have also same idea.

    But see the below code that i get it from your asp.net zero sample project.

    I have found two files in that you mention connection string name

    1) public class MCDbContext : AbpZeroDbContext<Tenant, Role, User> { /* Define an IDbSet for each entity of the application */

        public virtual IDbSet&lt;BinaryObject&gt; BinaryObjects { get; set; }
    
        /* Setting "Default" to base class helps us when working migration commands on Package Manager Console.
         * But it may cause problems when working Migrate.exe of EF. ABP works either way.         * 
         */
        public MCDbContext()
            : base("Default")
        {
    
        }
    
        /* This constructor is used by ABP to pass connection string defined in MCDataModule.PreInitialize.
         * Notice that, actually you will not directly create an instance of MCDbContext since ABP automatically handles it.
         */
        public MCDbContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {
    
        }
    }
    

    2) public class MCDataModule : AbpModule { public override void PreInitialize() { //web.config (or app.config for non-web projects) file should containt a connection string named "Default". Configuration.DefaultNameOrConnectionString = "Default"; }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
        }
    }
    

    If project is use connection string name from MCDbContext then our logic will definitely work. but i have checked its always connection string name is using from MCDataModule from PreInitialize() method. Because i have comment code in MCDbContext. then i also application is working.

    How can i pass connection string name dynamically in MCDataModule ? or If i can pass connection string name dynamically in MCDbContext then what is the setting in project for enabling get connection string name from MCDbContext not from MCDataModule.

    Awaiting for your response.

    Thanks, Jatin Gadhiya

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    Ignore setting it in MCDataModule and implement dynamic getting in the DbContext.

    Have a nice day.