Reputation: 25308
I am using EF4.1, have upgraded my project, generated POCO classes to use DbContext now and have a fun time - except for changing connection strings on the fly. This projects imports CSV files then merges the data into 2 (identical) databases. One DB is our PROD server, the other is our DEV server. I realize the way I was doing the change (below) no longer works as I switched to POCO.
What I was doing:
internal static Model.RIVFeedsEntities GetFeedsDB()
{
_serverName = "RivDB1";
// Create the dbZach database entity...
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
string connString = String.Format(@"metadata=res://*/Model.Feeds.csdl|res://*/Model.Feeds.ssdl|res://*/Model.Feeds.msl;provider=System.Data.SqlClient;provider connection string='Data Source={0};Initial Catalog={1};Integrated Security=True;MultipleActiveResultSets=True'"
, _serverName
, _databaseName);
entityBuilder.ConnectionString = connString;
entityBuilder.Metadata = "res://*/";
_sourceEntities = new Model.RIVFeedsEntities(entityBuilder.ConnectionString);
_sourceEntities.CommandTimeout = 60;
return _sourceEntities;
}
internal static Model.RIVFeedsEntities GetFeedsDBDev()
{
_serverName = "DB1";
// Create the dbZach database entity...
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
string connString = String.Format(@"metadata=res://*/Model.Feeds.csdl|res://*/Model.Feeds.ssdl|res://*/Model.Feeds.msl;provider=System.Data.SqlClient;provider connection string='Data Source={0};Initial Catalog={1};Integrated Security=True;MultipleActiveResultSets=True'"
, _serverName
, _databaseName);
entityBuilder.ConnectionString = connString;
entityBuilder.Metadata = "res://*/";
_sourceEntities = new Model.RIVFeedsEntities(entityBuilder.ConnectionString);
_sourceEntities.CommandTimeout = 60;
return _sourceEntities;
}
As you can see all I really need to do is change the SERVER portion of the connection string.
How do you do this using the DbContext object? I can see the base allows sending in a name or conn string in the constructor but DbContext itself does not and I am not seeing anything exposed.
TIA
Upvotes: 4
Views: 8703
Reputation: 6743
When you inherit from DbContext there are a few DbContext constructors from which you can override.
you may then choose from the different signature to find the one that suits you best.
in this forum http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/2efc32f7-23ad-4fad-84cf-279badb394a5
they are using either an SqlConnection object or the ConnectionString.
I think this still applies to the RTW version of EF 4.1
Upvotes: 1