Connecting to localdb from Entity Framework Core 1.x via a .Net Core Console Application

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


How can I use Entity Framework Core 1.x from a .NET Core 1.x Console Application inside of Visual Studio? I've tried the workflow and commands I'm used to from ASP.NET MVC Core 1.x development and I'm experiencing a connection error to the localdb.

How is working with localdb in a Console app different from working in an ASP.NET MVC Core 1.x app?


Having successfully used ASP.NET MVC Core 1.x and Entity Framework Core 1.x several times, I needed to create a simple .NET Core Console app to do some grunt work (load XML into a new database creating tables that loosely match the structure of the XML).

I used NuGet to install

  • Microsoft.EntityFrameworkCore.Design (1.1.2)
  • Microsoft.EntityFrameworkCore.SqlServer (1.1.2)
  • Microsoft.EntityFrameworkCore.Tools (1.1.1)

I created a DbContext with DbSets. I added an OnConfiguring() method and hard coded the connection string like I found in a code example online. My DbContext looks like this:

public class ApplicationDbContext : DbContext
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

    public DbSet<Author> Authors { get; set; }
    public DbSet<Thread> Threads { get; set; }
    public DbSet<Post> Posts { get; set; }

I grabbed the connection string from a working ASP.NET MVC Core 1.x application's appsettings.json, changing the database name to something simple ("MyXmlExport").

I was able to create an initial migration.

When I attempted to use the workflow I'm used to, namely calling:


I get the following error:

"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Specified LocalDB instance name is invalid."

I can connect to the localdb instance via Sql Server Object Explorer. I tried creating the MyXmlExport database manually, hoping that would reduce at least one potential issue. But it seems that Visual Studio's tooling can't access the database when used in the context of a Console application.

Full error stack:

Full error stack

5/1/2019 3:37:25 PM

Accepted Answer

The @ means you are using a verbatim string literal. You should not escape the \ between the server and instance name.

8/3/2017 5:35:23 PM

Popular Answer

I don't know if it helps Bob, but I'd consider the following:

Try stopping the LocalDb instance - enumerate the instances with

SqlLocalDb i 

From a cmd line...

Then try

SqlLocalDb stop <INSTANCE NAME>

See if that has any impact on it.

The flavour of the error suggests a connection string problem, so worth checking that the path to the MDF file is correct for each way you access it. I've had problems getting to grips with LocalDb connection strings in the past.

Probably not related but I'll throw it in here anyway, if you have attached the LocalDb MDF in SSMS, SSMS will trash the file permissions on the MDF when you detach it which means you might need to reset the permissions on the MDF and LDF files after detaching. Like I said - doesn't sound like this is the problem but might trigger a thought pattern for you.

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