![](/img/trans.png)
[英]Unable to add migration using Entity Framework and Visual Studio 2015
[英]How can I do an Entity Framework Core project and add a Migration in Visual Studio 2015 from nuget prompt?
在Visual Studio 2015中,我創建了一個ASP.net Core項目(以前稱為ASP.NET 5)。 該項目的模板創建了一個用於持久性的MS SQL localdb,以及實體框架和一些身份驗證表。
在我弄清楚如何瀏覽為此項目創建的localdb數據庫之后,我決定嘗試修改模型對象並嘗試使用ASP.NET MVC Web站點模板示例應用程序進行代碼優先遷移。 它使用EF為localdb提供登錄持久性。 我試過用了
演示應用程序已包含Migrations文件夾。 但是,如果您在包管理器控制台中鍵入add-migration SomeNameHere
,或者enable-migrations
則似乎無法使用示例項目的ER遷移。
我向IdentityModel.cs
單元添加了一個字符串屬性,我嘗試手動將其手動添加到0000...IdentitySchema.cs
文件中,但顯然我不知道如何正確地執行此操作,就像我運行時一樣應用程序,我遇到了一些錯誤,如下所示。 我相信我需要基本上使用Entity Framework代碼優先工具生成一些骨架.cs單元,它將進入Migrations文件夾。
人們建議現在做的通常做法是:
從包管理器控制台重新安裝看起來像這樣:
PM> Uninstall-Package EntityFramework
Uninstalling NuGet package EntityFramework.7.0.0-beta4.
Successfully uninstalled 'EntityFramework.7.0.0-beta4' from WebApplicationDNX.
PM> Install-Package EntityFramework -IncludePrerelease
Installing NuGet package EntityFramework.7.0.0-beta4.
Successfully installed 'EntityFramework.7.0.0-beta4' to WebApplicationDNX.
PM>
在這里,我相信我犯了一個錯誤,因為我正在嘗試運行像add-migration這樣的命令而我無法做到。 事實證明,我從未支持嘗試這樣的事情:
PM> add-migration DummyMigrate
add-migration : The term 'add-migration' is not recognized as the name of a cmdlet, function, script file, or operable program. ....
PM> enable-migration
enable-migration : The term 'enable-migration' is not recognized as the name of a cmdlet, function, script file, or operable program.
...
是否有可能從Visual Studio 2015, ASP.NET 5 Preview Templates -> Web Site
基於ASP.NET 5 Preview Templates -> Web Site
模板的應用程序中獲取實體框架遷移?
(更新1到4,因為它們用於無用的歷史測試版)
更新5:在asp.net核心1.0.0-RC2中,dnx工具已經消失,取而代之的是dotnet,但必須從你的項目源目錄而不是從你的解決方案目錄運行 dotnet ef
命令的基本原則,並且要做因此,您應該使用外部命令提示符或使用PowerShell Tools for Visual Studio,而不是nuget命令行提示符。
solution-dir project-source-dir
| |
d:\dev\AspNetCoreWebAp\src\AspNetCoreWebAp>dotnet ef
Project AspNetCoreWebAp (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing
Compiling AspNetCoreWebAp for .NETCoreApp,Version=v1.0
Compilation succeeded.
0 Warning(s)
0 Error(s)
Time elapsed 00:00:03.0846647
_/\__
---==/ \\
___ ___ |. \|\
| __|| __| | ) \\\
| _| | _| \_/ | //|\\
|___||_| / \\\/\\
Entity Framework .NET Core CLI Commands 1.0.0-preview1-20901
Usage: dotnet ef [options] [command]
以下內容僅適用於作為ASP.NET Core RC1一部分的dnx。 這已被替換,不適用於ASP.NET Core的一部分。
回答有關您正在尋找運行dnx命令的更好方法的問題:
我的dnx命令也給出了錯誤。
術語“dnx”未被識別為cmdlet的名稱,函數,
在我的Visual Studio包管理器控制台和Windows PowerShell中。
經過很多挫折之后,我能夠解決它。
以下是我為實現這一目標所采取的措施
導航到.dnx bin目錄
cd\\ cd users\\YOURUSERNAME\\.dnx\\bin
運行命令
.\\dnvm upgrade
如果出現錯誤消息“在此系統上禁用了腳本的執行”。 然后運行以下命令首先授予您權限
Set-ExecutionPolicy RemoteSigned
去打開Visual Studio。 然后,您將能夠在包管理器控制台和PowerShell中鍵入命令dnx
dnx
它會認出來的
你錯過了一步。 從您的beta 8處於活動狀態時的文檔中,缺少的命令將是:
Install-Package EntityFramework.Commands -Pre
使項目中的遷移命令可用。
Commands
被移動到一個單獨的包中,大概是為了使主框架更輕量級的生產,你不能使用,也不需要命令行實用程序。
答案 - 更新 - 由Warren提供:在Asp.net核心1.0.0-RC2中,相關軟件包名為Microsoft.EntityFrameworkCore.Tools
,它似乎是ASP.Net核心Web應用程序模板的一部分,使原始問題高度重要除非有人實際損害他們的project.json,否則不太可能發生。 不應從程序包管理器控制台運行命令行“EF”遷移命令,這不是DOTNET命令的一般環境。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.