简体   繁体   English

如何在Visual Studio 2015中管理EF 6迁移?

[英]How can I manage EF 6 migrations in visual studio 2015?

I started a new MVC project with EntityFramework -Version 6.1.2 using Visual Studio 2013 latest update. 我使用Visual Studio 2013最新更新启动了带有EntityFramework -Version 6.1.2的新MVC项目。 I made a couple of migrations and updated the database. 我进行了几次迁移并更新了数据库。 After this I checked out the project on another computer and opened with Visual Studio 2015 CTP 6. 之后,我在另一台计算机上签出了该项目,并使用Visual Studio 2015 CTP 6打开了该项目。

If I go in the package manager console and try to run any migration commands, they're not recognized: 如果我进入包管理器控制台并尝试运行任何迁移命令,它们将无法识别:

add-migrations : The term 'add-migrations' is not recognized as the name of a cmdlet, function, script file, or operable program. add-migrations:术语“ add-migrations”不被视为cmdlet,函数,脚本文件或可运行程序的名称。 Check the spelling of the name, or if a path was included, verify that the path is correct and try again. 检查名称的拼写,或者是否包含路径,请验证路径是否正确,然后重试。

How can I manage my migrations using Entity Framework 6 in Visual Studio 2015? 如何在Visual Studio 2015中使用Entity Framework 6管理迁移?

Have you tried uninstalling and reinstalling EntityFramwork NuGet package? 您是否尝试过卸载并重新安装EntityFramwork NuGet软件包? I had the same issue as described and it solved it. 我遇到了与上述相同的问题,并且解决了该问题。

OK so I had this problem with an EF6 project I started in VS2013 and upgraded to VS2015 RC. 好的,所以我在VS2013中启动并升级到VS2015 RC的EF6项目遇到了这个问题。 The commands were available if I opened it in VS2013 but not in VS2015. 如果我在VS2013中打开了这些命令,但在VS2015中没有打开。 What I found to solve the problem quickly and easily was to use the reinstall package command: 我发现可以快速,轻松地解决问题的方法是使用reinstall package命令:

Update-Package –reinstall EntityFramework

This fixed any command missing errors and then allowed me to start database updates and create new migrations within VS2015. 这修复了所有命令丢失的错误,然后允许我启动数据库更新并在VS2015中创建新的迁移。

I have done a few checks, but I'll recap trying to cover all the ef scenario: 我已经做了一些检查,但我将回顾一下所有ef场景:

ef7 in an asp.net 5 project : the migrations management has been changed and now is handled using the ef migration command. asp.net 5项目中的ef7 :迁移管理已更改,现在可以使用ef migration命令进行处理。
ef7 in a traditional .net application : it's required framework 4.5.1, but at this page esing ef 7 in a .net app you can find a good guide on how to use ef 7 in a traditional app. 传统.net应用程序中的ef7 :这是必需的框架4.5.1,但是在此页面上您可以在.net应用程序中使用ef 7,找到有关如何在传统应用程序中使用ef 7的很好的指南。 It's important to add the EntityFramework.Commands package. 添加EntityFramework.Commands包很重要。
ef6 : doesn't work with asp.net 5 , so even you can install it, the build will be broken. ef6 :不适用于asp.net 5 ,因此即使您可以安装它,构建也会被破坏。 Same situation for most of the actual ef drivers as the ones for mongodb and Postgresql. 大多数实际ef驱动程序的情况与mongodb和Postgresql的情况相同。 If you're not sure is a ef 7 compatible driver, don't install it. 如果您不确定是否与ef 7兼容,请不要安装它。 At the moment I think you can count just on sqlserver and inMemory. 目前,我认为您可以仅依靠sqlserver和inMemory。
for asp.net 4 applications, the actual installation of the commands is part of the entity framework package itself, so will be installed at the first installation of the package (as rightly pointed out above). 对于asp.net 4应用程序,命令的实际安装是实体框架包本身的一部分,因此将在该包的第一次安装时安装(如上正确指出的那样)。
In some situations, in an environment where ef package has not been installed, copying an asp.net 4 project and starting to work on it, may give this issue, that magically disappears when for whatever reason the package is installed. 在某些情况下,在尚未安装ef软件包的环境中,复制asp.net 4项目并开始对其进行处理可能会出现此问题,无论出于何种原因安装了软件包,魔术地消失了。

I was having similar issues in that I could not run Add-Migration from the Package Manager Console, either (though I was getting a different error.) Unfortunately, reinstalling EntityFramework was not working for me, perhaps because I had ASP.Net 5 projects in the same solution. 我遇到类似的问题,因为我也无法从Package Manager控制台运行“ Add-Migration ”(尽管我遇到了另一个错误。)不幸的是,重新安装EntityFramework不适用于我,可能是因为我有ASP.Net 5项目在相同的解决方案中。 (This was not for a traditional csproj , as it requires resx's which are not supported as far as I know.) (这不适用于传统的csproj ,因为它需要resx,据我所知,不支持。)

I ended up creating a console application, referencing the project that included my migrations, and added the following: 我最终创建了一个控制台应用程序,引用了包含我的迁移的项目,并添加了以下内容:

    var configuration = GetConfiguration();

    var scaffolder = new MigrationScaffolder(configuration);

    scaffolder.Namespace = configuration.MigrationsNamespace;

    var scaffoldedMigration = scaffolder.Scaffold(name);

    System.IO.File.WriteAllText(scaffoldedMigration.MigrationId + ".cs", scaffoldedMigration.UserCode);
    System.IO.File.WriteAllText(scaffoldedMigration.MigrationId + ".Designer.cs", scaffoldedMigration.DesignerCode);
    System.IO.File.WriteAllText(scaffoldedMigration.MigrationId + ".resx", BuildResx(scaffoldedMigration.Resources));

GetConfiguration returns an instance of your *.Migration.Configuration class. GetConfiguration返回*.Migration.Configuration类的实例。 name is the new name of your migration. name是您的迁移的新名称。 This code by default drops it into the Debug folder of your console app; 默认情况下,此代码将其放入控制台应用程序的Debug文件夹中。 drag-and-drop from there into Visual Studio in your Migration folder and it should do the trick. 从那里拖放到Migration文件夹中的Visual Studio中,它应该可以解决问题。

I later discovered that this was basically the same as https://stackoverflow.com/a/20382226/195653 but for completely different reasons. 后来我发现这与https://stackoverflow.com/a/20382226/195653基本相同,但是出于完全不同的原因。

I did all combinations of reinstall or uninstall and install EntityFramework plus closing and reopening VS. 我完成了重新安装或卸载,安装EntityFramework以及关闭和重新打开VS的所有组合。 The only thing that worked was deleting the folder packages in my solution folder and rebuild the solution to force all packages to download. 唯一有效的方法是删除我的解决方案文件夹中的文件夹软件包,然后重建解决方案以强制所有软件包下载。

Update-Package EntityFramework ...应该可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Visual Studio 2015将迁移应用于生产服务器? - How apply migrations to the production server using Visual Studio 2015? 无法在Visual Studio 2015 Web API项目中启用EF6迁移 - Unable to Enable EF6 Migrations in Visual Studio 2015 Web API project Visual Studio 2015和实体框架迁移不起作用 - Visual Studio 2015 and Entity Framework Migrations not working 如何检查进程是否由 Visual Studio > 2015 启动 - How can I check if a process was started by Visual Studio > 2015 如何为Visual Studio 2015社区安装EF电动工具 - how to install EF power tools for visual studio 2015 community Visual Studio 2015:使用 oracle 设置 EF6 - visual studio 2015: setup EF6 with oracle 同时引用EF 5和EF 6项目的Visual Studio 2015项目 - Visual Studio 2015 project that references both EF 5 and EF 6 projects Visual Studio 2015 RC实体框架6.1.3迁移错误 - Visual Studio 2015 RC Entity Framework 6.1.3 Migrations Error 如何通过代码直接使用 EF Core Migrations API - How can I use EF Core Migrations API directly by code 如何使用托管标识从 Visual Studio 运行 EF6 代码优先迁移 - How to run EF6 code-first migrations from Visual Studio with Managed Identity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM