I know this question has been asked and answered a few times already, but every answer I have encountered has either been incomplete, overly vague or assumes more prerequisite knowledge than I have. I am seeking step-by-step instructions, in reasonably plain English, to swap out the database in my default connection string for a database name that will be looked up in a table. I keep coming across this snippet of code...
public DFDBEntities(string connectionString)
: base(connectionString)
{
}
...but no one really explains how, and where, to implement it. Assuming I have a class called "db" that represents my DataModel.EDMX model...
private DataModel db = new DataModel();
...how would I change the connection string associated with that class to point to a different physical database (with identical data structure)?
By using the default constructor you are basically saying "I want to use the default connection string" which is specified in the web.config.
The DbContext has a constructor overload which allows you override the default connection string, all you need to do is pass it in
db = new DataModel("MyConnectionStr");
Try :
private DataModel db = new DataModel(yourconnectionString);
You can use helper class for connenction settings like
public class EntityConnectionStringHelper
{
public static string Build()
{
string serverName = "******";
string databaseName = "****";
string password = "****";
string userid = "*****";
string meta = "********";
return EntityConnectionStringHelper.Build(serverName, databaseName, userid, password, meta);
}
public static string Build(string serverName, string databaseName, string userName, string password, string metaData)
{
// Specify the provider name, server and database.
string providerName = "System.Data.SqlClient";
return EntityConnectionStringHelper.Build(providerName, serverName, databaseName, userName, password, metaData);
}
public static string Build(string providerName, string serverName, string databaseName, string userName, string password, string metaData)
{
// Initialize the connection string builder for the
// underlying provider.
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
// Set the properties for the data source.
sqlBuilder.DataSource = serverName;
sqlBuilder.InitialCatalog = databaseName;
sqlBuilder.IntegratedSecurity = false;
sqlBuilder.UserID = userName;
sqlBuilder.Password = password;
// Build the SqlConnection connection string.
string providerString = sqlBuilder.ToString();
// Initialize the EntityConnectionStringBuilder.
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
//Set the provider name.
entityBuilder.Provider = providerName;
// Set the provider-specific connection string.
entityBuilder.ProviderConnectionString = providerString;
// Set the Metadata location.
entityBuilder.Metadata = string.Format(#"res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", metaData);
return entityBuilder.ToString();
}
}
Than for your entity you can get intance with
DFDBEntities context = new DFDBEntities (EntityConnectionStringHelper.Build());
Related
I'm trying to pass a connection string to DbContext class through it's constructor but I keep getting this exception. System.ArgumentException: 'Keyword not supported: 'provider'.'
Based on other stackoverflow posts I'm getting the connection string this way.
public string GetConnection()
{
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
sqlBuilder.DataSource = "DataSource";
sqlBuilder.InitialCatalog = "InitialCatalog";
sqlBuilder.UserID = "UserID";
sqlBuilder.Password = "Password";
EntityConnectionStringBuilder entityString = new EntityConnectionStringBuilder()
{
Provider = "System.Data.SqlClient",
ProviderConnectionString = sqlBuilder.ToString()
};
return entityString.ConnectionString;
}
This is what is generated (actual credentials replaced with dummy data)
provider=System.Data.SqlClient;provider connection string="Data Source=DataSource;Initial Catalog=InitialCatalog;User ID=UserID;Password=Password"
This is the DBContext class constructor
public CustomerContext(string connectionString) : base(connectionString)
{
}
For anyone who that might be running into this error. Turns out I had to install the EF6 packages on all projects that were going to reference my repository dll.
Once the packages are installed on all projects that need it, I passed in the connection string only. Without any special formatting or providing the Provider info.
Data Source=DataSource;Initial Catalog=InitialCatalog;User ID=UserID;Password=Password
I have a project that uses entity framework data model(.edmx) in it's data layer.I want to add work station id in connection string to store it during create log in database.
This is what I do:
var d = new PresentModelConnectionString();
string connectionString = d.Database.Connection.ConnectionString;
string lastCharacter = connectionString.Substring(connectionString.Length - 1, 1);
if (lastCharacter == ";")
{
connectionString += $"workstation id={Helpers.UserId.ToString()}";
}
else
{
connectionString += $";workstation id={Helpers.UserId.ToString()}";
}
d.Database.Connection.ConnectionString = connectionString;
return d;
But when it tries to connect to database and get data returns The login for user sa failed.When I remove this line:
d.Database.Connection.ConnectionString = connectionString;
It works fine.
This is the connection string:
<add name="PresentModelConnectionString" connectionString="metadata=res://*/PresentModel.csdl|res://*/PresentModel.ssdl|res://*/PresentModel.msl;provider=System.Data.SqlClient;provider connection string="data source=192.168.1.101\sql2014;initial catalog=MIS;user id=sa;password=sa_123;connect timeout=600000000;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
How can I include work station id dynamically to connection string?
Thanks
After searching several hours I found this solution, it change the connection string of entity framework database first model.As Alex recommended in comments I should use SqlConnectionStringBuilder to make connection string.
Create a partial class for model context in a separate file for adding constructor that gives connection string as parameter:
public partial class PresentModelConnectionString
{
public PresentModelConnectionString(string connectionString):base(connectionString)
{
}
}
Make connection string:
//connection string in web.config
//Data Source=192.168.1.101\sql2014;Initial Catalog=MIS_Keshavarzi_980906;user id=sa;pwd=sa_123; Connect Timeout=60000;
string connectionString =
System.Configuration.ConfigurationManager.AppSettings["ABSConnectionString"];
SqlConnectionStringBuilder connectionStringBuilder = new
SqlConnectionStringBuilder(connectionString);
connectionStringBuilder.WorkstationID = Helpers.UserId.ToString(); //get work station id
connectionStringBuilder.ApplicationName = "EntityFramework"; //set application name
For modifying the model connection string use EntityConnectionStringBuilder:
EntityConnectionStringBuilder entityConnectionBuilder = new
EntityConnectionStringBuilder();
entityConnectionBuilder.Metadata =
"res://*/PresentModel.csdl|res://*/PresentModel.ssdl|res://*/PresentModel.msl";
entityConnectionBuilder.Provider = "System.Data.SqlClient";
entityConnectionBuilder.ProviderConnectionString =
connectionStringBuilder.ConnectionString;
PresentModel should change with your model name.
Finally create a new instance of model context with this connection string:
var entityContext = new
PresentModelConnectionString(entityConnectionBuilder.ConnectionString);
In Asp.net Entity framework, we use both Model first, DB first for same database,
And there is two connection string for Model first, DB first.
So how can we create single connection string for both Model first, DB first.
You can use EntityConnectionStringBuilder:
string sqlConnectionString = "Data Source=localhost;
Initial Catalog=AdventureWorks;Integrated Security=True;Connection Timeout=60;multipleactiveresultsets=true"
string providerName = "System.Data.SqlClient";
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.Provider = providerName;
entityBuilder.ProviderConnectionString = sqlConnectionString;
entityBuilder.Metadata = #"res://*/AdventureWorksModel.csdl|
res://*/AdventureWorksModel.ssdl|
res://*/AdventureWorksModel.msl";
string entityConnectionString = entityBuilder.ToString();
Or, if you are brave enought, you can try just reformat entityConnectionString:
string sqlConnectionString = "Data Source=localhost;
Initial Catalog=AdventureWorks;Integrated Security=True;Connection Timeout=60;
multipleactiveresultsets=true"
string entityConnectionString =
string.Format("metadata=.\AdventureWorks.csdl|.\AdventureWorks.ssdl|.\AdventureWorks.msl; provider=System.Data.SqlClient;provider connection string='{0}'" providerName=\"System.Data.EntityClient\"",
sqlConnectionString
);
Edit:
Alternatively, you can take oposite approach, lets keep EntityConnectionString and extract SqlConnectionString from it:
string entityConnectionString = 'xxxxxxx';
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder(entityConnectionString);
string sqlConnectionString = entityBuilder.ProviderConnectionString;
The connection strings will be different: the DB first one will have the metadata [embedded] location at the beginning. You could put the "normal" Code First connection string in the web.config and add the "metadata part" in the DB First DbContext constructor, using a static method to build the EntityConnectionString needed.
Example:
public MyContext() : base(BuildConnectionString())
{
}
private static string BuildConnectionString()
{
varsqlBuilder = new SqlConnectionStringBuilder();
//dinamically prepare the provider connection string here, using the CF one in the web.config
//[..]
//now build the entity connection string, using the one just built as ProviderConnectionString
var entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.ProviderConnectionString = sqlBuilder.ToString();
entityBuilder.Metadata = "res://*/";
entityBuilder.Provider = "System.Data.SqlClient";
return entityBuilder.ToString();
}
I was given a method to get our database connection string to Sql Server:
SqlConnection GetConnectionString()
I call that and get what the connection string should be. If the database does not exist, I need the connection string without the database name in it. If I try to use the connection string with the database name in it, I get an error that it cannot connect to the database, which is it since it does not exist.
I am calling like this:
using (var connection = new SqlConnection(GetConnectionString().ConnectionString))
Is there a way to recreate the connection string easily without the database name?
SqlConnectionStringBuilder aBuilder =
new SqlConnectionStringBuilder(yourConnectionStringWithDatabase);
aBuilder.InitialCatalog = "";
string yourConnectionStringWithoutDatabase = aBuilder.ConnectionString;
It's easy
var connectionString = "data source=someInstance;initial catalog =someDatabase;etc.";
var pattern = "initial catalog[=\\s\\w]+;";
var dbRemoved = Regex.Replace(connectionString, pattern, "");
Note that I haven't handled case sensitivity, but this should be a good start for your requirements.
In my project I have a method which needs to return a ConnectionStringSettings object. As the database and server name will change dynamically, I need to dynamically
construct the connection string.
How do I convert a string to ConnectionStringSettings?
public ConnectionStringSettings getConnection(string server, string database)
{
//ConnectionStringSettings connsettings = new ConnectionStringSettings();
string connection = ConfigurationManager.ConnectionStrings["myConnString"].ToString();
connection = string.Format(connection, server, database);
// Need to convert connection to ConnectionStringSettings
// Return ConnectionStringSettings
}
--Web.config
<add name="myConnString" connectionString="server={0};Initial Catalog={1};uid=user1;pwd=blah; Connection Timeout = 1000"/>
The ConnectionStringSettings class constructor has an overload that takes two strings (first is the name of the connection string and the second is the connection string itself).
public ConnectionStringSettings getConnection(string server, string database)
{
string connection = ConfigurationManager.ConnectionStrings["myConnString"].ToString();
connection = string.Format(connection, server, database);
return new ConnectionStringSettings("myConnString", connection);
}
There's a third overload that takes in an extra string for the name of the provider.