Was hoping maybe you could point me in the right direction as I seem to be at a block. I'd like to know how to implement this extension class? Where does usage code go? I'm using Prism and my DbContext is in another project.
edit
I changed my code to reflect that link below, I'm confused on the usage as it seems my code is not using the new generated connection string. When I put in a invalid server name, my application is still able to retrieve data. I should not be able to get data with a bad server name.
public static class ConnectionTools
{
// all params are optional
public static void ChangeDatabase(
this DbContext source,
string initialCatalog = "",
string dataSource = @"",
string userId = "",
string password = "",
bool integratedSecuity = true,
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
}
}
}
Usage
public void LogonClick(object sender, RoutedEventArgs e)
{
DialogResult = true;
// assumes a connectionString name in .config of MyDbEntities
var ADataEntities = new ADataEntities();
// so only reference the changed properties
// using the object parameters by name
ADataEntities.ChangeDatabase
(
initialCatalog: "",
userId: "",
password: "",
// user Entered DataSource is the only change needed
dataSource: (DataSource)
);
Close();
}
After the usage code above I verify the UserName in my DB and my DbContext is still using the original connection string in App.config.
public void Authorize(string userName, string password)
{
var AuthContext = new ADataEntities();
var query = from c in AuthContext.UserNames
where (c.UserID == userName && c.EmailAddress == password )
select c;
{