Hi @Dean Everhart ,
Changing location of database .mdf file from default in c:\users\username
You can try the following steps and code:
- In the appsetting.json file, use the following connection string to create the database. "MVC6SampleDbConnection": "Server=(localdb)\mssqllocaldb;AttachDbFilename=MVC6SampleDB;Trusted_Connection=True;MultipleActiveResultSets=true"
After migration, we can find the database file (MVC6SampleDB.mdf and MVC6SampleDB_log.ldf) from the "c:\users\username" folder. - Copy the all the database file from the "c:\users\username" folder to the project folder.
- Change the connection string in the appsetting.json file as below: "MVC6SampleDbConnection": "Server=(localdb)\mssqllocaldb;AttachDbFilename=[DataDirectory]\Database\MVC6SampleDB.mdf;Trusted_Connection=True;MultipleActiveResultSets=true"
- In the Program.cs file, after getting the connection string, replace the
[DataDirectory]
to the application directory. string path = Directory.GetCurrentDirectory();
var connectionString = builder.Configuration.GetConnectionString("MVC6SampleDbConnection").Replace("[DataDirectory]", path);
After that, you can add new Entity and generate the table into the database in the project folder via the EF core migration. And you can also use SSMS to view the database:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion