Entity Framework Code First Migration Error

2020-02-24 12:26发布

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

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.

14条回答
Animai°情兽
2楼-- · 2020-02-24 12:39

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

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

查看更多
Explosion°爆炸
3楼-- · 2020-02-24 12:39

Uninstalling Entity Framework didn't work for me.

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

Entity Framework and Visual Studio require 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

查看更多
成全新的幸福
4楼-- · 2020-02-24 12:44

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

<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:

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

I could then Enable-Migrations with no issues.

查看更多
走好不送
5楼-- · 2020-02-24 12:48

As of version 6.3, the Entity Framework tooling supports the new SDK-style project format.

However, this issue can still happen when the project containing your migrations uses the new SDK-style format, while the startup project is using the classic csproj format.

To fix this, make both projects use the same csproj format.

查看更多
放荡不羁爱自由
6楼-- · 2020-02-24 12:49

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 ParameterBindingValidationException when 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

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.

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.

查看更多
地球回转人心会变
7楼-- · 2020-02-24 12:53

Visual studio 2019: Uninstall-Package EntityFramework -Force

then

Install-Package EntityFramework -Pre -Version 6.2.0

something is up with v6.3.0

查看更多
登录 后发表回答