如何在 nuget 提示符下执行 Entity Framework 核心项目并在 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 核心项目(以前称为ASP.NET 5)。该项目的模板创建了一个 MS SQL localdb 用于持久性,以及 entity framework 和一些身份验证表。

在弄清楚如何浏览为该项目创建的 localdb 数据库后,我决定尝试修改模型对象并尝试使用 ASP.NET MVC 网站模板示例应用程序进行代码优先迁移。它使用 EF 为 localdb 提供登录持久性。我尝试使用

演示应用程序已包含迁移文件夹。但是,如果您在程序包管理器控制台中键入 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 基于模板的应用程序进行 entity framework 迁移?

(更新 1 到 4 已删除,因为它们用于无用的历史测试版)

更新 5:在 asp.net 核心 1.0.0-RC2 中,dnx 工具已经消失,取而代之的是 dotnet,但是 dotnet ef 命令的基本原则 必须是运行 来自您的项目源目录 而不是来自您的解决方案目录,为此,您可能应该使用外部命令提示符或使用 PowerShell 工具 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]

你错过了一步。从您的 Beta 8 处于活动状态时的 documentation 开始,缺少的命令应该是:

Install-Package EntityFramework.Commands -Pre to make the migrations commands available in your project.

Commands 被移动到一个单独的包中,大概是为了使主框架更轻量级用于生产,您不能使用也不需要命令行实用程序。

Answer-Update-contributed-by-Warren:在 Asp.net 核心 1.0.0-RC2 中,相关包被命名为 Microsoft.EntityFrameworkCore.Tools,它似乎是 ASP.Net 的一部分核心 Web 应用程序模板使得原始问题极不可能发生,除非有人真正破坏了他们的 project.json。命令行 "EF" 迁移命令不应该是来自包管理器控制台的 运行,这不是 DOTNET 命令的一般环境。

以下仅适用于 ASP.NET Core RC1 的一部分的 dnx。这已被替换并且不适用 ASP.NET 核心部分。

回答有关您正在寻找 运行 dnx 命令的更好方法的问题:

我的 dnx 命令也给出了错误。

The term 'dnx' is not recognized as the name of a cmdlet, function,

在我的 Visual Studio 包管理器控制台和 Windows powershell 中。

经过多次挫折,我终于解决了它。

以下是我为使其正常工作所做的步骤

  • 关闭 Visual Studio
  • 的所有实例
  • 删除 C:\Users\YOURUSERNAME\.dnx 中的 .dnx 文件夹。我的超过 1 GB
  • 打开 Visual Studio 2015。创建一个新的默认模板 ASP.NET 5 Web 应用程序。构建并 运行 它。这将重新创建 .dnx 文件夹。之后文件夹大小将近600MB
  • 打开 Powershell
  • 导航到 .dnx bin 目录

    cd\
    cd users\YOURUSERNAME\.dnx\bin
    
  • 运行命令

    .\dnvm upgrade
    

    如果你有错误信息"execution of scripts is disabled on this system."那么运行下面的命令先给你权限

    Set-ExecutionPolicy RemoteSigned
    

打开Visual Studio。然后,您将能够在包管理器控制台和 powershell

中键入命令 dnx
   dnx

它会识别它