Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core: Scaffold DbContext keeps failing

The official ASP.Net Core says that the following error can be fixed by restarting visual studio:

 Scaffold-DbContext : The term 'Scaffold-DbContext' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

However the command keeps failing.

I'm running it in Package Manager console following the example provided in ASP.Net core online documentation.

Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
like image 238
Arnold Zahrneinder Avatar asked Oct 02 '16 12:10

Arnold Zahrneinder


2 Answers

This is what I begin with when creating new ASP.NET Core 1.1 project which saves me lot of time. If you complete Step 1 and Step 2 did not work immediately, try restarting Visual Studio and continue to Step 2.

Step 1

Use the following as project.json. Restore packages.

{
  "version": "1.0.0-*",

  "dependencies": {
    "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
    "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.0",
    "Microsoft.EntityFrameworkCore": "1.1.0",
    "Microsoft.EntityFrameworkCore.Design": "1.1.0",
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
    "NETStandard.Library": "1.6.1",
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.1.0"
    }
  },
  "frameworks": {
    "netcoreapp1.1": {
      "imports": "dnxcore50"
    }
  },
  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.1.0-preview4-final",
    "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.1.0-preview4-final"
  }
}

Step 2

Run your query in Package Manager Console. The query below connects to TestDb db in TestServer

Scaffold-DbContext "Data Source=TestServer;Initial Catalog=TestDb;Persist Security Info=True;User ID={Username};Password={Password}" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -force -v -t dbo.Table1, dbo.Table2, dbo.Table3
like image 131
Mausam Avatar answered Oct 15 '22 06:10

Mausam


I've encountered the same problem. For me it was EntityFrameworkCore.Tools that was missing and did not properly install through NuGet.

Run these commands in your package manager console (Tools > NuGet Package Manager > Package Manager Console):

Install-Package Microsoft.EntityFrameworkCore.SqlServer –Pre 
Install-Package Microsoft.EntityFrameworkCore.Tools –Pre 
Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design –Pre

And then in project.json I added this:

"tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
}

Important! Make sure this version matches "dependencies" package (also in project.json). In this example "1.1.0-preview4-final" should also be used in dependencies.

If the problem still persists, try restoring your ef runtimes, guidance found here: https://github.com/aspnet/EntityFramework/issues/5549

like image 42
Nick De Beer Avatar answered Oct 15 '22 04:10

Nick De Beer