Skip to content Skip to sidebar Skip to footer

Dynamicly Change Database Mvc3 And Entityframework 4.1

I am working on an MVC3 application database first approach . I would like to use one connection string to connect to database, based on some string (company name). Example: I have

Solution 1:

Connecting to different Databases is best done using the DBconnection constructor on DBCOntext. If you look at the DBContext class you will see multiple constructor overloads. One allows the DBConnection to be supplied. So no entry in WEB.Config/App.Config is required.

See this post with sample code Same Context accessing different databases.

EDIT sample added:

public partial classobracun_placEntities1 : DbContext
{
    // use THIS CONSTRUCTORprotectedobracun_placEntities1(DbConnection dbConnection, bool contextOwnsConnection)
        : base(dbConnection, contextOwnsConnection)
    {
    }

     }
}

// DONT USE THIS//    obracun_placEntities1.nameOrConnectionString = entityBuilder.ToString();//        obracun_placEntities1 o = new obracun_placEntities1(entityBuilder.ToString());`// build the connection - note: it is NOT a connection string. it is a DBConnection!

   conn = getDBConnection4SQLServer(DatabaseServer,Databasename)
   obracun_placEntities1 o = newobracun_placEntities1(conn,true);

 //====================================================================publicconststringDefaultDataSource = "localhost";

 publicDbConnectiongetDBConnection4SQLServer(string dataSource, string dbName) {
        var sqlConnStringBuilder = newSqlConnectionStringBuilder();
        sqlConnStringBuilder.DataSource = String.IsNullOrEmpty(dataSource) ? DefaultDataSource : dataSource;
        sqlConnStringBuilder.IntegratedSecurity = true;
        sqlConnStringBuilder.MultipleActiveResultSets = true;

        var sqlConnFact = newSqlConnectionFactory(sqlConnStringBuilder.ConnectionString);
        var sqlConn = sqlConnFact.CreateConnection(dbName);
        return sqlConn;
    }

Solution 2:

I finnaly getting it to work with this code.

My LoginController

    [HttpPost]
    public ActionResult Index(UPORABNIK model, FormCollection form)
    {....}

    publicobracun_placEntities1(EntityConnection entityConnection)
        : base(entityConnection, false)
    {
    }

I call EntityConnection conn = GetEntityConnDbName("ServerName", "FirmName"). _entities = new obracun_placEntities1(conn, false);

var uporabniki = from r in _entities.UPORABNIK.Where(r => r.ime == uporabnik && r.geslo == geslo && danes <= r.veljavnost).ToList() select r;

I get the firmName from the post form so this work only in LoginController.But how can I use this constructor in all other Controllers? I get firm name only once in LoginControler, I tried to save it as cookie but then i can not read it in the constructor. In my other Controller I use the default controller again. How can I tranfer conn to other Controllers?

private obracun_placEntities1 db = new obracun_placEntities1();

I would like to call like this

private obracun_placEntities1 db = new obracun_placEntities1(conn);

Or is there some better way?

Post a Comment for "Dynamicly Change Database Mvc3 And Entityframework 4.1"