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

How can I use Entity Framework Core 1.x from a .NET Core 1.x Console application in Visual Studio? I've tried the workflow and commands I'm used to from ASP.NET MVC Core 1.x and I ran into a localdb connection error.

How does working with localdb in a console app differ from working in an ASP.NET MVC Core 1.x app?

Background

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 application to do some work (load XML into a new database by creating tables that are free conform to the XML structure).

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 hardcoded the connection string as I found in the sample code online. My DbContext looks like this:

public class ApplicationDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(@"Server=(localdb)\\mssqllocaldb;Database=MyXmlExport;Trusted_Connection=True;MultipleActiveResultSets=true");
    }

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

      

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

I managed to create an initial migration.

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

update-database

      

I am getting the following error:

"There was a network-related or instance-specific error while establishing a connection to SQL Server. The server was not found or was not available. Make sure the instance name is correct and that SQL Server is configured to allow remote connections. (Provider: SQL Network Interfaces, error: 50 - A local database error has occurred. The specified LocalDB instance name is not valid. "

I can connect to my localdb instance via Sql Server Object Explorer. I tried to create the MyXmlExport database manually, hoping that this would reduce at least one potential problem. But it looks like Visual Studio tools cannot access the database when used in the context of a console application.

Full error stack:

Full error stack

+3


source to share


2 answers


@

means you are using a string string literal. You shouldn't avoid \

between server and instance name.



+7


source


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

Try to stop the LocalDb instance - list the instances with

SqlLocalDb i 

      

From the cmd line ...

Then try



SqlLocalDb stop <INSTANCE NAME>

      

See if it affects this.

An error tinge indicates a problem with the connection string, so it's worth checking that the path to the MDF file is correct for each way you access it. I've had problems in the past with binding to LocalDb connection strings.

Probably unrelated, but I'll put it here anyway, if you've connected MDF LocalDb in SSMS, SSMS will destroy the MDF file permissions when you detach it, which means you might need to reset the MDF and LDF file permissions after detaching. As I said - doesn't seem like a problem, but may trigger a thought pattern for you.

0


source







All Articles