简体   繁体   中英

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

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?

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 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)
    {
        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 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:

update-database

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:

完整的错误堆栈

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM