Passing Connection String to Entity Framework 6

You need to introduce another constructor in your context that is expecting a string connectionString argument and make it call base(string nameOrConnectionString):

public cerviondemoEntities(string connectionString) : base(connectionString)
{
}

I have used the connection string like this, entity connection string instead of normal connection string

  SqlConnectionStringBuilder sqlString = new SqlConnectionStringBuilder()
        {
    DataSource = "SOURAV-PC", // Server name
    InitialCatalog = "efDB",  //Database
            UserID = "sourav",         //Username
            Password = "mypassword",  //Password
        };
        //Build an Entity Framework connection string

        EntityConnectionStringBuilder entityString = new EntityConnectionStringBuilder()
        {
            Provider = "System.Data.SqlClient",
            Metadata =   "res://*/testModel.csdl|res://*/testModel.ssdl|res://*/testModel.msl",
            ProviderConnectionString = sqlString.ToString()
        };
        return entityString.ConnectionString;
    }

I had this issue as well and used the method from Daniel in the comments.

Alternatively, you can add it to the .tt file instead of creating another file and using 'partial' – Daniel K Dec 9 '16 at 19:16

*Update .Context.tt File

just replace the lines...

    public <#=code.Escape(container)#>()
    : base("name=<#=container.Name#>")
{

with the following...

public <#=code.Escape(container)#>()
    : this("name=<#=container.Name#>")
{
}

public <#=code.Escape(container)#>(String nameOrConnectionString)
    : base(nameOrConnectionString)
{

By convention, Entity Framework takes the connection string that has the same name as the context. For example:

public cerviondemoEntities()
    : base("name=cerviondemoEntities")
{
}

The DbContext class has a constructor that takes a connection string. You can add another constructor that takes a connectionstring as a parameter and pass it to the base constructor.

public cerviondemoEntities(string connectionString) : base(connectionString)
{
}

Be sure to create a partial class so your added constructor is not overwritten.

Sample ConnectionString:

<connectionStrings>
    <add name="cerviondemoEntities" connectionString="data source=server\database;initial catalog=catalog;persist security info=True;user id=user;password=password;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /> 
</connectionStrings>