Base solution for your next web application
Open Closed

Change Configuration.DefaultNameOrConnectionString #177


User avatar
0
agunghehe created

Hi,

Is there any way to change Configuration.DefaultNameOrConnectionString in the Application or Core? I have a logic that need to switch that property to a new one, rather than already stated in the EntityFramework. i am expecting by changing this property i could easily switch to different database.

Thanks in advance mate.

Regards.


3 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    You can inject IAbpStartupConfiguration from anywhere and get/set DefaultNameOrConnectionString property.

    Have a nice day.

  • User Avatar
    0
    agunghehe created

    Hi,

    I just understand if IAbpStartupConfiguration is a singleton. Does it mean, it will exist in entire application? I mean the same property will be used in all users?

    Is there any possibility a user has its own connection string to be injected in AbpDbContext? How to do that?

    Many thanks.

    Regards,

  • User Avatar
    0
    hikalkan created
    Support Team

    Yes, it's singleton. You want to change conn string dynamically. So, check your DbContext class, you will see that it gets and passes connection string to AbpDbContext (base class). In this point, you can somehow pass the connection string dynamically. Say that your dbcontext is like that:

    public class AbpProjectNameDbContext : AbpDbContext
    {
        //...
    
    	public AbpProjectNameDbContext()
    		: base("Default")
    	{
    
    	}
    }
    

    "Default" is the conn string name in web.config. It can be the conn string itself. So, you can create a class to get conn string dynamic upon your rules, inject it to AbpProjectNameDbContext and use it's GetConnString method to pass conn string to base. So, how this new conn string provider class get conn according to current user? It's completely your implementation based on your needs. ABP infrastructure does not provide a way or prevent you doing anything (for example you can get from user's session).

    Have a nice day.