locked
Dynamically switching datasource to a new database of exact same structure RRS feed

  • Question

  • Does anyone have any thoughts about using the EntityClient Provider for the Entity Framework in order to programmatically change the database referenced for a particular datasource. Assume the database will be the exact same structure as the entity model. Obviously this would be a great concept for changing datasources to say - perhaps a database dedicated to training, production, testing etc. I know the consensus is to simply deploy a separate LS project tied to a different datasource but there must be some options around this....

    This is a very common practice across commercial software vendors....

    Is LS up to the task???

    • Edited by NGCBSiegel Thursday, October 6, 2011 3:21 AM
    Thursday, October 6, 2011 3:19 AM

Answers

  • What do you mean programmatically? A different database for a different user logging on for example? In general you could do this using a RIA Services data source that uses an EF model and a domain service to expose your entities. If you override your domain service's CreateDataContext you can do this.

    something Like that:

     

     

        protected override MyEntities CreateObjectContext() {
          return new MyEntities(GetConnectionString(System.Threading.Thread.CurrentPrincipal.Identity.Name));
        }
    
    

    Where GetConnectionString will return the proper connection string based on the user. This way you can even have multi-tenant applications.

     

     


    1+1 = 3 for large values of 1
    Friday, October 7, 2011 1:15 PM

All replies

  • What do you mean programmatically? A different database for a different user logging on for example? In general you could do this using a RIA Services data source that uses an EF model and a domain service to expose your entities. If you override your domain service's CreateDataContext you can do this.

    something Like that:

     

     

        protected override MyEntities CreateObjectContext() {
          return new MyEntities(GetConnectionString(System.Threading.Thread.CurrentPrincipal.Identity.Name));
        }
    
    

    Where GetConnectionString will return the proper connection string based on the user. This way you can even have multi-tenant applications.

     

     


    1+1 = 3 for large values of 1
    Friday, October 7, 2011 1:15 PM
  • Very similiar,

    In the solutions we develop, we have table named “company” housed in a primary system database. This company table holds rows referencing multiple companies a user can work with when they log into the LS application. Each row holds company specific information relative to the database the company data is maintained in.  For example: company name, SQL Server, DB alias, DB port, DB owner, DB password, etc… With this information, we dynamically change the primary DB connection string used through the entire work session within the application. If the user wants to change companies (in our world), they log out and log back into application selecting a different company. You can assume all company databases are of equal structure. So the primary challenge, dynamically changing the data source (connection string) at runtime when the LS project is run.

    Saturday, October 8, 2011 4:16 AM
  • I believe that the RIA way you can do that. Not very simple but I am sure it can be done. I will work on it and come back with more. I am sure this can be done.

    1+1 = 3 for large values of 1
    Saturday, October 8, 2011 6:11 AM
  • I have come up with a solution that is not exactly what you want but is very similar. In my world :-) if the user wants to login to a different company, defines the company he wants to use as a user setting he logs out and the next time he logs in the selected company is used. Would that be acceptable? I can provide a sample with this solution if you want.
    1+1 = 3 for large values of 1
    Saturday, October 8, 2011 7:58 AM
  • In our systems, companies/divisions/departments are a segment in the GL organization code.  There are two other segments in the GL:  Physical Location and the GL Account Number.

    A corportation can configure rolle based user access based on Location and Organization codes.  Standard ERP requirements.

    A user accesses a particular database and application based on the web site they go to.  A single database supports multiple applications with aspnet security.

    If a user was to access multiple corporate sites they would have separate Desktop icons on their desktop (or separate favorite settings in the Browser).

    I am intrigued to understand your business requirements to have a user switch databases based on their login.

    Monday, October 10, 2011 7:34 PM