Changing location of database .mdf file from default in c:\users\username

Dean Everhart 1,536 Reputation points
2022-12-01T16:05:27.433+00:00

Question Setup

1. My tutorial Core 6 project folder is:

C:\Users\User1\Desktop**SolutionName\ProjectName"

2. The default connection string in the Core6 introductory tutorials...

  },  
  . . .   
  "ConnectionStrings": {  
    "ProjectNameContext": "Server=(localdb)\\mssqllocaldb;Database=**ProjectName.Data**;Trusted_Connection=True;MultipleActiveResultSets=true"  
  }  

...locate the database (.mdf) (and database log (.ldf)) files at the following path:

C:\Users\UserName**ProjectName.Data.mdf**

Question:

How do I have the database created to the project folder at C:\Users\User1\Desktop**SolutionName\ProjectName" (small database size - just for working with tutorials)? In other words, I want the project folder to contain it's own database.

Additional Information:

If I create multiple iterations of the project that all have the same connection string, they all point to a single database at C:\Users\UserName**ProjectName.Data.mdf**. As properties are added in the database in successive iterations of a tutorial project, earlier iterations of the project stop working because the database schema has been changed.

Thank you for any help you can provide.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,663 questions
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,546 Reputation points Microsoft Vendor
    2022-12-02T05:56:58.95+00:00

    Hi @Dean Everhart ,

    Changing location of database .mdf file from default in c:\users\username

    You can try the following steps and code:

    1. 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.
    2. Copy the all the database file from the "c:\users\username" folder to the project folder. 266415-image.png
    3. 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"
    4. 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: 266390-image.png

    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


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.