如何从nuget提示符执行Entity Framework Core项目并在Visual Studio 2015中添加迁移?

War*_* P 17 powershell entity-framework visual-studio-2015 dnx asp.net-core

在Visual Studio 2015中,我创建了一个ASP.net Core项目(以前称为ASP.NET 5).该项目的模板创建了一个用于持久性的MS SQL localdb,以及实体框架和一些身份验证表.

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

演示应用程序已包含Migrations文件夹.但是,如果您add-migration SomeNameHere在Package Manager控制台中键入内容,或者enable-migrations似乎无法使用示例项目使用ER Migrations.

我在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> 
Run Code Online (Sandbox Code Playgroud)

在这里,我相信我犯了一个错误,因为我正在尝试运行像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. 
...
Run Code Online (Sandbox Code Playgroud)

是否有可能从基于ASP.NET 5 Preview Templates -> Web Site模板的应用程序中获取Visual Studio 2015中的实体框架迁移?

(更新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]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

dev*_*ric 22

以下内容仅适用于作为ASP.NET Core RC1一部分的dnx.这已被替换,不适用于ASP.NET Core的一部分.

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

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

术语"dnx"未被识别为cmdlet的名称,函数,

在我的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
    
    Run Code Online (Sandbox Code Playgroud)
  • 运行命令

    .\dnvm upgrade
    
    Run Code Online (Sandbox Code Playgroud)

    如果出现错误消息"在此系统上禁用了脚本的执行".然后运行以下命令首先授予您权限

    Set-ExecutionPolicy RemoteSigned
    
    Run Code Online (Sandbox Code Playgroud)

去打开Visual Studio.然后,您将能够在包管理器控制台和PowerShell中键入命令dnx

   dnx
Run Code Online (Sandbox Code Playgroud)

它会认出来的

在此输入图像描述

  • 谢谢.我已经按照相同的步骤,我现在可以运行DNX命令. (2认同)

Cla*_*ies 4

你错过了一步。从beta 8 激活时的文档来看,缺少的命令是:

Install-Package EntityFramework.Commands -Pre 使迁移命令在您的项目中可用。

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

Answer-Update-contributed-by-Warren:在 Asp.net core 1.0.0-RC2 中,相关包被命名Microsoft.EntityFrameworkCore.Tools,它似乎是 ASP.Net Core Web 应用程序模板的一部分,使得原始问题极不可能发生,除非有人实际上损坏了他们的project.json。命令行“EF”迁移命令不应从程序包管理器控制台运行,这不是 DOTNET 命令的通用环境。

  • 此答案在 Visual Studio 2015 更新 1 的 EF7 中不再有效。我必须转到命令提示符并从命令提示符运行 `dnx ef migration....` 命令 (3认同)