If the database is created dynamically during the program execution, then by default it is created on the c \ mssqlserver \ disk, etc.
How to dynamically create a database with the desired name in the right folder?
2 answers
If dynamically, from code, then it seems to be so (specify the path to the database in the same place as the EXE):
string executable = Assembly.GetExecutingAssembly().Location; string path = (Path.GetDirectoryName(executable)); AppDomain.CurrentDomain.SetData("DataDirectory", path); In general, look towards App.config , the connectionStrings parameter: by default, the connection string should have the same name as the context file, but in addition to the name, you can also specify the location of the database itself:
< connectionStrings> < add name="SampleContext" providerName="System.Data.SqlClient" connectionString="Server=.\SQLEXPRESS;Database=MyBase;Trusted_Connection=true"/> < /connectionStrings> If you have already created DBContext , then in the code you can switch the database as follows:
context.Database.Connection.ChangeDatabase(databaseName);
You can also specify the path to the database in the context constructor:
PS The studio is not at hand, just can not check.
When creating a database context, you can specify either the name of the connection string parameter of the configuration file (Web.Config, or App.config)
< connectionStrings> < add name="SampleContext" providerName="System.Data.SqlClient" connectionString="Server=.\SQLEXPRESS;Database=MyBase;Trusted_Connection=true"/> < /connectionStrings> or by redefining the constructor
public class Db : DbContext { public Db() : base("SampleContext") { } public Db(string connectionStringName):base(connectionStringName){} }