Changing your connection string at runtime with Spring.Net

Something that pops up every once in a while on the Spring.Net user forum is how to change your connection string at runtime. You need this behaviour for instance when you have a multi tenant application.

Out of the box Spring.Net comes with two DbProviders to change information about your connection string at runtime. One provider to change the credentials at runtime named UserCredentialsDbProvider, and one that can choose a connectionstring from a predefined list called MultiDelegatingDbProvider.

If you want to change the connection string at runtime, for instance based on the current logged in user, what works best is to either subclass DelegatingDbProvider or UserCredentialsDbProvider (which actually is a subclass of DelegatingDbProvider).

Attached you can find a quick and dirty sample which shows what you need to hook up.

RuntimeConnectionString.zip (8.29 kb)