Visual Studio 2017 using EF Core change local database default location for mdf file before/after migration

c# ef-migrations entity-framework-core visual-studio-2017


I may need help with the title of my question. So here is more detail.

In VS2015 I when creating a database using code first migrations, After completing my first migration I was able to view the database in VS by going to the App_Data folder and clicking on the MDF file. It would then open in Server Explorer.

However, in VS2017 I no longer have an App_Data folder and when I did my first migration it took a bit of work to find out where my MDF file was located.

If you don't know here are the steps I used to locate it:

After doing the add-migration and update-database.

Go to View->SQL Server Object Explorer

Under SQL Server look for you (localdb)\MSSQLLocalDB->Databases->name of your databases should show in your appsettings.json connection string. Probably starts with aspnet-[db name]-[bunch of numbers and letters]

I right clicked on this database and went to properties. Under "Current Connection Parameters" is the path to the MDF file.

The current location is "C:/user/username"

So my question is how do I set this to a different default location? I would like my MDF files to show in the Data folder or something similar that is in my project folder.

If this question has been asked. I apologize I tried rewording my question about 20 times to figure out if someone already asked it. I will quickly remove this question if it has already been asked in a way that is unfamiliar to me.

4/30/2017 7:15:13 PM

Accepted Answer

Ok, so for Entity Framework Core, it is a bit more involved. You can open your db in SQL Server Object Explorer in Visual Studio (or in Sql Management Studio) and create your database where you want it using a SQL query.

create database test on (name='test', filename='c:\Projects\test.mdf');

And then reference it using (LocalDb) the way you normally would in the connection string:


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

And then this test runs correctly


using System;
using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

namespace ConsoleApp1
    class Program
        static void Main(string[] args)
            Console.WriteLine("Hello World!");

            var builder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

            IConfigurationRoot configuration = builder.Build();

            var optionsBuilder = new DbContextOptionsBuilder();


            var context = new DbContext(optionsBuilder.Options);


So you're still using the same server, but you're placing the database in the folder you want.

In action: enter image description here

4/30/2017 8:16:14 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow