I am migrating an existing Web Application (using Entity Framework 5) to an Azure Web Role.
The database connection string is being moved from the web.config
to the ServiceConfiguration.*.cscfg
files.
The problem is that in the auto-generated Model.Context.cs
file, my entities class is defined like this:
public partial class MyEntities : DbContext
{
public MyEntities()
: base("name=MyEntities")
{ }
// DbSets, etc
}
This will always look for MyEntities
in the web.config
. How can I override this constructor so that I can pass in the connection string from the ServiceConfiguration.*.cscfg
file?
I could derive from this class, like so:
public class MyCloudEntities : MyEntities
{
public MyCloudEntities()
: base(CloudConfigurationManager.GetSetting("MyEntities"))
{ }
}
But then I have to change every instantiation of MyEntities
in the code base, and it wont prevent developers from using MyEntities
in the future.