You need to build a connectionstring like this:
SqlConnectionStringBuilder sqlBldr = new SqlConnectionStringBuilder();
sqlBldr.DataSource = "Server name";
sqlBldr.InitialCatalog = "Database name";
sqlBldr.IntegratedSecurity = true; // or use use + password
sqlBldr.MultipleActiveResultSets = true;
EntityConnectionStringBuilder entBldr = new EntityConnectionStringBuilder();
entBldr.ProviderConnectionString = sqlBuilder.ToString();
entBldr.Metadata = "res://*/";
entBldr.Provider = "System.Data.SqlClient";
The EF connection string is entityBuilder.ToString()
. You simply has to use it in your DbContextBuilder
. Expose the inherited constructor which receives one parameter:
public class MyDbContext
{
public MyDbContext(strinc connStr)
: base(connStr)
{
}
// ...
}
Then use it like this:
string connectionString = // ... use your connection string builder for this
using(MyDbContext ctx = new MyDbContext(connectionString))
{
// ...
}
For choosing file names and location
Note that you can specify the full path to the .mdf
file in the AttachDBFilename property of your SqlConnectionStringBuilder
property instead of using server + database. See SqlConnectionStringBuilder MSDN docs for more information.
However, you must ensure that, uf the user specifies a folder, she must have read/write access to that folder.