Visual Studio 2017 using EF Core change local database default location for mdf file before/after migration
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:
appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=test;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
And then this test runs correctly
Program.cs
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()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
IConfigurationRoot configuration = builder.Build();
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
var context = new DbContext(optionsBuilder.Options);
context.Database.EnsureCreated();
}
}
}
So you're still using the same server, but you're placing the database in the folder you want.
In action: