DbContext: use other connection strings - EntityFramework

1

I have a web.config file with 4 connection strings. How can I put these connection strings in class DbContext ?

public DataContext():base("ConnectionString") // Essa é minha conexão padrão
{

}

How can I do the same with another connection?

    
asked by anonymous 30.07.2014 / 23:22

1 answer

2

When you create the connection strings in web.config you pass names to them in the name property. For you to choose one in your DbContext implementation, simply pass the name of the connection as it is in the name property in web.config to the base class constructor. It's like you did, but with the name of the other connections.

EDIT: An example would be the following, you add in web.config the following connection string

<connectionStrings>
    <add name="Exemplo" connectionString="ConnectionString" providerName="Provider" />
</connectionStrings>

Since ConnectionString is your connection string and Provider your provider, which in this case and SQL Server is System.Data.SqlClient . Hence in your context, you would do the following

public class ContextoExemplo : DbContext
{
    public ContextoExemplo() : base("Exemplo") {}
}

Then this context will sweat the connection string with name Example you added. Each context would simply have that over there what goes in the name property and EF knows what to use that connection string.

    
30.07.2014 / 23:26