2016-10-02 84 views
4

官方ASP.Net睿說,下面的錯誤可以通過重新啓動Visual Studio的固定:EF核心:腳手架的DbContext不停地進行故障

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. 

然而,命令不斷失敗。

我按照ASP.Net核心聯機文檔中提供的示例在Package Manager控制檯中運行它。

Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models 
+0

請嘗試在PowerShell窗口中執行命令(首先轉到項目文件夾) –

回答

1

我遇到了同樣的問題。對於我來說,它是EntityFrameworkCore.Tools缺失,並沒有通過NuGet正確安裝。

運行在你的包管理器控制檯這些命令(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 

然後在project.json我加了這一點:

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

重要!確保此版本匹配「依賴關係」包(也在project.json中)。在這個例子中,「1.1.0-preview4-final」也應該用在依賴項中。

如果問題仍然存在,嘗試恢復您的EF運行時,指導在這裏找到:https://github.com/aspnet/EntityFramework/issues/5549

2

這是我開始創建新的ASP.NET 1.1的核心項目,爲我節省了大量的時間時。如果在完成步驟1和步驟2沒有馬上工作,請嘗試重新啓動Visual Studio和繼續執行步驟2

步驟1

使用以下爲project.json。還原包。

{ 
    "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" 
    } 
} 

步驟2

運行在軟件包管理器控制檯查詢。下面的查詢連接到TestServer的TestDb db

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