Click here to Skip to main content
14,422,431 members
Rate this:
Please Sign up or sign in to vote.
See more:
I want to create database in a folder in my project, but by default the database is created in C:\Users\USER.

This is my appsetting.json :


"ConnectionStrings": {
"MvcMusicStoreContext":
"Server=(localdb)\\mssqllocaldb;Database=MvcMusicStoreContext;Trusted_Connection=True;MultipleActiveResultSets=true"

}
How to change database location to another folder in my computer. I use asp.net core 2.2 in Visual Studio 2017


What I have tried:

I use asp.net core 2.2 in Visual Studio 2017
Posted
Updated 12-Jan-20 5:11am
v2
Comments
Richard MacCutchan 11-Jan-20 5:35am
   
Use a full path rather than just a filename in your connection string.
Reza kavian 11-Jan-20 5:59am
   
Can you give me an example of how to do it?
Rate this:
Please Sign up or sign in to vote.

Solution 1

If you are using the (localdb) designation then the file will be stored in the default location as described at Creating a Connection String and Working with SQL Server LocalDB | Microsoft Docs[^]. For other options see SQL Server Connection Strings for ASP.NET Web Applications | Microsoft Docs[^].
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

i change ConnectionStrings with AttachDBFileName:

"ConnectionStrings": {<br />
    "MvcMusicStoreContext":<br />
    "Server=(localdb)\\MSSQLLocalDB;AttachDBFilename=[DataDirectory]\\App_Data\\MvcMusicStoreContext.mdf;Trusted_Connection=True;MultipleActiveResultSets=true"<br />
  }

and in ConfigureServices in startup:

string path = Directory.GetCurrentDirectory(); <br />
<br />
  services.AddDbContext<MvcMusicStoreContext>(options =><br />
                  options.UseSqlServer(Configuration.GetConnectionString("MvcMusicStoreContext")<br />
                  .Replace("[DataDirectory]",path)));
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100