Entity Framework – How to supply a connection string to constructor

If you are using Entity Framework in your projects or websites. You are about to add clients which will have other databases with other connection strings. Database schema is same for all.

For this you are to pass client connection string in EF constructor, this will override the base connection string and use the connection string database.

Basic connection string will not works with EF so you have to use same format of connection string which was created while creating the EDMX file i.e with meta info.

There are two ways to use connection string in constructor:

1. EntityConnectionStringBuilder : Create a connection string format real time with all details.

2. Connection String from config file.

using (MyEntity myent= new MyEntity("name=client2_connectionstring"))
{
}

using (MyEntity myent= new MyEntity("name=client2_connectionstring"))
{
}