.net 实体框架代码第一次迁移错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14410987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:50:59  来源:igfitidea点击:

Entity Framework Code First Migration Error

.netentity-frameworkexception-handlingef-code-first

提问by Rafferty

I'm new to MVC 4 and entity framework and when I run this command from the package manager console:

我是 MVC 4 和实体框架的新手,当我从包管理器控制台运行此命令时:

Enable-Migrations -ContextTypeName MyFirstMvcApp.Models.InventoryDbContext

Join-Path : Cannot bind argument to parameter 'Path' because it is null.

加入路径:无法将参数绑定到参数“路径”,因为它为空。

I get the following error:

我收到以下错误:

Join-Path : Cannot bind argument to parameter 'Path' because it is null.
At D:\GitProjects\MyFirstMvcApp\trunk\packages\EntityFramework.6.0.0-alpha2\tools\EntityFramework.psm1:363 char:27
+     $toolsPath = Join-Path <<<<  $installPath tools
    + CategoryInfo          : InvalidData: (:) [Join-Path], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCommand

Join-Path : Cannot bind argument to parameter 'Path' because it is null.
At D:\GitProjects\MyFirstMvcApp\trunk\packages\EntityFramework.6.0.0-alpha2\tools\EntityFramework.psm1:392 char:73
+     $utilityAssembly = [System.Reflection.Assembly]::LoadFrom((Join-Path <<<<  $toolsPath EntityFramework.PowerShell.Utility.dll))
    + CategoryInfo          : InvalidData: (:) [Join-Path], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCommand

You cannot call a method on a null-valued expression.
At D:\GitProjects\MyFirstMvcApp\trunk\packages\EntityFramework.6.0.0-alpha2\tools\EntityFramework.psm1:393 char:50
+     $dispatcher = $utilityAssembly.CreateInstance <<<< (
    + CategoryInfo          : InvalidOperation: (CreateInstance:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Join-Path : Cannot bind argument to parameter 'Path' because it is null.
At D:\GitProjects\MyFirstMvcApp\trunk\packages\EntityFramework.6.0.0-alpha2\tools\EntityFramework.psm1:426 char:19
+         (Join-Path <<<<  $runner.ToolsPath EntityFramework.PowerShell.dll),
    + CategoryInfo          : InvalidData: (:) [Join-Path], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCommand

How do I fix this? Note that I am using Visual Studio 2010 Ultimate with SP1 and I have installed MVC4 in this link.

我该如何解决?请注意,我使用的是带有 SP1 的 Visual Studio 2010 Ultimate,并且我已在此链接中安装了 MVC4 。

回答by Rafferty

Fixed it by uninstalling and re-installing entity framework. For the benefit of others, here are the commands...

通过卸载并重新安装实体框架来修复它。为了他人的利益,这里是命令...

Uninstall:

卸载:

PM> Uninstall-Package EntityFramework -Force

PM> Uninstall-Package EntityFramework -Force

After uninstalling, reinstall EntityFramework:

卸载后,重新安装EntityFramework:

PM> Install-Package EntityFramework -Pre

PM> Install-Package EntityFramework -Pre

There! It's fixed. Though I still don't know what caused the problem.

那里!它是固定的。虽然我仍然不知道是什么导致了问题。

回答by Ayson Baxter

Visual studio 2019: Uninstall-Package EntityFramework -Force

Visual Studio 2019:Uninstall-Package EntityFramework -Force

then

然后

Install-Package EntityFramework -Pre -Version 6.2.0

安装包 EntityFramework -Pre -Version 6.2.0

something is up with v6.3.0

v6.3.0 有问题

回答by RAGHAVENDRA

I was facing the same issue this is how I solved the issue,

我遇到了同样的问题,这就是我解决问题的方法,

I'm using Visual Studio 2019 and hence I installed latest Entity framework 6.3.0(stable) I was facing the issue when I run the Enable-Migrations command, I tried to re-install the Entity framework but it didn't work so I have installed the old version 6.2.0 from the Nuget package manger apparently it turned out working.

我正在使用 Visual Studio 2019,因此我安装了最新的实体框架 6.3.0(stable) 我在运行 Enable-Migrations 命令时遇到了这个问题,我尝试重新安装实体框架,但它没有用我已经从 Nuget 包管理器安装了旧版本 6.2.0 显然它可以工作。

回答by Nemesis

Tried all the things here. I personally hate solving things without knowing what the problem is in the first place. Digging down in forums this seems to be a known bug caused in version 6.3.0 when the startup project is a web app. They have already fixed it in their daily builds. So to solve it you need to install that.

尝试了这里的所有东西。我个人讨厌在不知道问题是什么的情况下解决问题。在论坛中挖掘这似乎是在启动项目是 Web 应用程序时在 6.3.0 版本中引起的已知错误。他们已经在日常构建中修复了它。所以要解决它,你需要安装它。

First, uninstall the package:

首先,卸载软件包:

PM> Uninstall-Package EntityFramework -Force

Then install the latest daily build (in my case the following one)

然后安装最新的每日构建(在我的情况下是以下一个)

PM> Install-Package EntityFramework -Version 6.4.0-preview1-19506-01

回答by Raqeeb A.

I came across this issue while upgrading to EntityFramework "6.3.0" from "6.2.0". The issue and workaround can be found here EF 6.3.0 PMC commands throw ParameterBindingValidationExceptionwhen Startup Project is a Web App.

我在从“6.2.0”升级到 EntityFramework“6.3.0”时遇到了这个问题。问题和解决方法可以在这里找到EF 6.3.0 PMC commands throw ParameterBindingValidationExceptionwhen Startup Project is a Web App

The problem arises when you have multiple projects and you set an ASP.NET project as the Startup project

当你有多个项目并且你将一个 ASP.NET 项目设置为 Startup 项目时就会出现问题

Workaround suggested by the link:

链接建议的解决方法:

If you added a new dummy console app to the solution and installed the daily build of the EntityFramework into it, that version of the commands which includes the fix would be used. The commands would work just fine with projects referencing EF 6.3.

如果您向解决方案添加了一个新的虚拟控制台应用程序并将 EntityFramework 的每日构建安装到其中,则将使用包含修复程序的命令版本。这些命令适用于引用 EF 6.3 的项目。

Personal Solution:

个人解决方案:

Setting the Startup project to a non ASP.NET project fixed the issue in my case. Note that you will have to copy the connection strings to the relevant project's configuration file if you're using them.

将启动项目设置为非 ASP.NET 项目解决了我的问题。请注意,如果您正在使用连接字符串,则必须将它们复制到相关项目的配置文件中。

回答by Tobias

Some years later...
Another possible reason for this issue is the VS 2017 csproj format - or better said, the PackageReference approach in contrast to using packages.config.

几年后......
这个问题的另一个可能原因是 VS 2017 csproj 格式 - 或者更好地说,PackageReference 方法与使用 packages.config 形成对比。

With the new approach to include Nuget Packages using the PackageReference format in your project files, the install script of EntityFramework is not being executed (it is a known fact for this new approach of including nuget packages). Due to that fact, the PowerShell commands are not being ready for execution.

使用在项目文件中使用 PackageReference 格式包含 Nuget 包的新方法时,不会执行 EntityFramework 的安装脚本(这是包含 nuget 包的这种新方法的已知事实)。因此,PowerShell 命令尚未准备好执行。

There is an ongoing discussion on that issue on the Entity Framework GitHub repository.
Until this is fixed (either by the NuGet people or the EF people) in order for migrations to run (talking of EF 6 - not EF Core!) you need to stick to the "old" way of including Nuget packages using the packages.config approach.

Entity Framework GitHub 存储库上正在讨论该问题。
在修复(由 NuGet 人员或 EF 人员)以运行迁移(谈论 EF 6 - 而不是 EF Core!)之前,您需要坚持使用包包含 Nuget 包的“旧”方式。配置方法。

Check your Visual Studio settings at
Tools -> Options -> NuGet Package Manager -> Generaland make sure that the
Default package management formatis set to Packages.config.

检查您的 Visual Studio 设置
Tools -> Options -> NuGet Package Manager -> General并确保
Default package management format设置为Packages.config.

Note that you will have to uninstall all Packages, save the project, restart visual studio and afterwards re-install all packages when switching this setting.
In current Version of VS 2017 (I think from v 15.4 onwards) there is an automatic conversion from Packages.config to PackageReference, but not vice versa (unfortunately).

请注意,切换此设置时,您必须卸载所有包,保存项目,重新启动 Visual Studio,然后重新安装所有包。
在 VS 2017 的当前版本中(我认为从 v 15.4 开始)有从 Packages.config 到 PackageReference 的自动转换,但反之亦然(不幸的是)。

回答by Bellash

I had the same problem and solved it by changing the Startup Project.

我遇到了同样的问题并通过更改Startup Project.

I have 5 project in the same solution and each time I Right Clickand Set as startupto a non ASPNET CORE project, I get this kind of error.

我在同一个解决方案中有 5 个项目,每次我Right ClickSet as startup一个非 ASPNET CORE 项目时,我都会遇到这种错误。

回答by Karim slaimi

i had the same problem when i updated EntityFramework to version 6.3.0 so when i went back to 6.2.0 it fixed the problem so i advice you to do the same

当我将 EntityFramework 更新到 6.3.0 版时遇到了同样的问题,所以当我回到 6.2.0 时它解决了这个问题,所以我建议你也这样做

回答by Matt

Uninstalling Entity Framework didn't work for me.

卸载实体框架对我不起作用。

It turned out that it was not Visual Studioor Entity Framemworkcasuing the problem, but it was the fact I was running PowerShell 2(on Windows 7).

原来,这不是Visual StudioEntity Framemworkcasuing的问题,但它是我跑的事实PowerShell 2(上Windows 7)。

Entity Frameworkand Visual Studiorequire PowerShell 3.

Entity FrameworkVisual Studio要求PowerShell 3.

Following the update and reboot, the issue was gone. Hopefully this will help a few people.

更新并重新启动后,问题消失了。希望这会帮助一些人。

You can find PowerShell here -> https://www.microsoft.com/en-us/download/details.aspx?id=40855

您可以在此处找到 PowerShell -> https://www.microsoft.com/en-us/download/details.aspx?id=40855

回答by The Senator

In my case none of the above solutions worked, so I had a quick look in the packages.config file and to my surprise found two entries for Entity Framework

就我而言,上述解决方案均无效,因此我快速查看了 packages.config 文件,令我惊讶的是发现实体框架的两个条目

<package id="EntityFramework" version="6.1.1" targetFramework="net45" />
<package id="EntityFramework" version="6.1.3" targetFramework="net45" />

Note that they are different versions, not sure how this could actually happen. Anyway, once I removed the older one from the packages.config file i.e. ended up with just this one:

请注意,它们是不同的版本,不确定这实际上是如何发生的。无论如何,一旦我从 packages.config 文件中删除了旧的,即最终得到了这个:

<package id="EntityFramework" version="6.1.3" targetFramework="net45" />

I could then Enable-Migrations with no issues.

然后我可以毫无问题地启用迁移。