I apologize for posting this as a question, but I'm not able to comment under the actual solution to my question yet, which was answered here. This solution also doesn't work in a same way.
I used that solution and the extension seems to work itself apart from actually changing the connection. It remains same as it is defined in web.config
file. If I remove that connection string I get error saying that EF couldn't find it.
My approach is Database first (moreover, it's SQL Server 2000...) and EF version 6 (basically, the latest)
So my question is - how it supposed to work?
- Do I have to pass same connection name to the extension method as it is defined in
web.config
or should it be different?
My current connection string looks as follows:
<connectionStrings>
<add name="CATALOGEntities" connectionString="metadata=~/bin/Models\InfoModel.csdl|~/bin/Models\InfoModel.ssdl|~/bin/Models\InfoModel.msl;provider=System.Data.SqlClient;provider connection string="data source=SERVER;initial catalog=CATALOG;integrated security=False;User Id=admin;Password=admin123;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
ATTEMPT 1: This is what I'm passing to extension method:
ConnectionTools.ChangeDatabase(db, "ANOTHERCATALOG", "ANOTHERSERVER", "admin", "admin456", false, "ANOTHERCATALOGEntities");
ATTEMPT 2: Tried as suggested by VDohnal too:
db.ChangeDatabase("ANOTHERCATALOG", "ANOTHERSERVER", "admin", "admin456", false, "ANOTHERCATALOGEntities");
ATTEMPT 3 with:
public partial class CATALOGEntities : DbContext {
public CATALOGEntities(string connectionString) : base(connectionString) { }
public CATALOGEntities() {
// TODO: Complete member initialization
Database.SetInitializer<CATALOGEntities>(null);
}
}
ATTEMPT 4: Doesn't work either (assuming me having 2 connection strings defined in web.config
(source)):
if (infoWhole.QueryDetails.IsCountryUK)
{
string strConn = ConfigurationManager.ConnectionStrings["CATALOGEntities"].ConnectionString;
db = new CATALOGEntities(strConn);
}
else
{
string strConn = ConfigurationManager.ConnectionStrings["CATALOGEntitiesUSA"].ConnectionString;
db = new CATALOGEntities(strConn);
}
- Also, what data source should I pass to extension method - the whole
DbContext
or the one defined in the controller class I'm working in, which isCATALOGEntities
?
Here is the extension method that I'm using:
public static class ConnectionTools
{
// all params are optional
public static void ChangeDatabase(
this CATALOGEntities source,
string initialCatalog = "",
string dataSource = "",
string userId = "",
string password = "",
bool integratedSecuity = false,
string configConnectionStringName = "")
/* this would be used if the
* connectionString name varied from
* the base EF class name */
{
try
{
// use the const name if it's not null, otherwise
// using the convention of connection string = EF contextname
// grab the type name and we're done
var configNameEf = string.IsNullOrEmpty(configConnectionStringName)
? source.GetType().Name
: configConnectionStringName;
// add a reference to System.Configuration
var entityCnxStringBuilder = new EntityConnectionStringBuilder
(System.Configuration.ConfigurationManager
.ConnectionStrings[configNameEf].ConnectionString);
// init the sqlbuilder with the full EF connectionstring cargo
var sqlCnxStringBuilder = new SqlConnectionStringBuilder
(entityCnxStringBuilder.ProviderConnectionString);
// only populate parameters with values if added
if (!string.IsNullOrEmpty(initialCatalog))
sqlCnxStringBuilder.InitialCatalog = initialCatalog;
if (!string.IsNullOrEmpty(dataSource))
sqlCnxStringBuilder.DataSource = dataSource;
if (!string.IsNullOrEmpty(userId))
sqlCnxStringBuilder.UserID = userId;
if (!string.IsNullOrEmpty(password))
sqlCnxStringBuilder.Password = password;
// set the integrated security status
sqlCnxStringBuilder.IntegratedSecurity = integratedSecuity;
// now flip the properties that were changed
source.Database.Connection.ConnectionString
= sqlCnxStringBuilder.ConnectionString;
}
catch (Exception ex)
{
// set log item if required
}
}
}
My DbContext:
public partial class CATALOGEntities : DbContext
{
public CATALOGEntities()
: base("name=CATALOGEntities")
{
}
}