As already mentioned, you can declare your connection string inside the config file of your application with a name (let's say "YourDBName") and then pass this to the DbContext
base constructor call (I will add this to the answer for providing a complete answer - great answers already given on this).
Alternatively, you can set this programmatically in your DbContext
Extension class, using the Database.Connection.ConnectionString
property. For instance:
App.config:
<!-- More.... -->
<!-- You can do this in a declarative way -->
<connectionStrings>
<add name="YourDBName"
connectionString="<Your connection string here>"
providerName="<Your provider here>" />
</connectionStrings>
<!-- More.... -->
DatabaseContext.cs:
public class DatabaseContext : DbContext
//Link it with your config file
public DatabaseContext () : base("YourDBName")
{
//And/Or you can do this programmatically.
this.Database.Connection.ConnectionString = "<Your Connection String Here>";
// More Stuff.....
}
}
public MyContext() : base("NameForTheDB") { }
– Dita