none
is there anyway to dynamically connect to different database using entity framework

    Question

  • sceniro is user login , system to check which company database ,this user is belong to ,then to connect this company's database.

    so my question is how to dynamically connect to different database using entity framework?

    Thursday, November 29, 2012 11:57 PM

Answers

  • If you see the Model.Designed.cs, you will find a constructor accepting connection string like below -

    So upon user Authentication - Create the ObjectContext instance by passing the connection string.

    Hope this will help you.


    Dhananjay Tech Lead

    • Marked as answer by Alexander Sun Friday, December 07, 2012 1:28 AM
    Friday, November 30, 2012 3:35 AM
  • // Build phase

    Build the DbCompiledModel explicitly by youself, then new the DbContext with the model

    DbModelBuilder modelBuilder = new DbModelBuilder();
    DbModel @new = modelBuilder.Build(new SqlConnection(connectionString));
        DbCompiledModel compiledModel = @new.Compile();

     

    //Deleteing phase

    var dbContext = new DbContext(connection, model, true);


    SAY NEVER

    • Marked as answer by Alexander Sun Friday, December 07, 2012 1:28 AM
    Friday, November 30, 2012 6:27 AM

All replies

  • If you see the Model.Designed.cs, you will find a constructor accepting connection string like below -

    So upon user Authentication - Create the ObjectContext instance by passing the connection string.

    Hope this will help you.


    Dhananjay Tech Lead

    • Marked as answer by Alexander Sun Friday, December 07, 2012 1:28 AM
    Friday, November 30, 2012 3:35 AM
  • // Build phase

    Build the DbCompiledModel explicitly by youself, then new the DbContext with the model

    DbModelBuilder modelBuilder = new DbModelBuilder();
    DbModel @new = modelBuilder.Build(new SqlConnection(connectionString));
        DbCompiledModel compiledModel = @new.Compile();

     

    //Deleteing phase

    var dbContext = new DbContext(connection, model, true);


    SAY NEVER

    • Marked as answer by Alexander Sun Friday, December 07, 2012 1:28 AM
    Friday, November 30, 2012 6:27 AM