We are writing single page app (SPA) in which users connect to different database after successful log in.
When user is authenticated and authorized, their “profile” will contain ldatabse id that they can connect to…
My questions revolve around Entity Framework and ASP.NET MVC (and Web API)
- Is it possible to connect to a different DB with EF at run time (after user logs in). I cannot use connection string from web.config as it will vary…Any examples if this is possible would be appreciated….
- Since creating connection is expensive operation are there any performance optimizations I should consider? Should I try to create a pool of connections for DB upon first user’s login or should I create pools for each DB I have in advance (hmmm…)?
If the different databases have the same structure and you can use the same model then you should be able to connect to different databases. Both ObjectContext and DbContext have constructor that takes string that can be a connection string. Note that ObjectContext expects the entity connection string which contains names/locations of artifacts.
I believe connections are automatically pooled. I would not try to do any optimization here unless tests show that this is causing performance problems.