Enable Migrations in MVC throws File not Found Error - c#

I'm new to using MVC, and when working through the tutorials regarding migrations in Visual Studio 2013 I appear to have broken the tie-in to EntityFramework in the Package Manager console.
I believe this started when I updated the EntityFramework to version 6.1.3, But that appears to be incompatible with the MVC Controller scaffolding. So I rolled it back to version 5.0.0.
When I now enter the command enable-migrations
I get the following error:
Exception calling "CreateInstanceFrom" with "8" argument(s): "Could not load file or assembly 'EntityFramework, Version=5.0.0.0
, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified."
At D:\ss\CAP\CAP_MVC_Dev_2\CAP_MVC\packages\EntityFramework.5.0.0\tools\EntityFramework.psm1:431 char:31
+ $domain.CreateInstanceFrom <<<< (
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodTargetInvocation
The file EntityFramework.psm1 exists where it is described above, but the error appears to be in that file? How is $domain.CreateInstanceFrom missing, and where from? Uninstalling and reinstalling the EntityFramework package makes no difference.
I've tried creating new projects from scratch and this error persists across them. An uninstall/reinstall of Visual Studio 2013 has not resolved the issue either, nor has destroying the connected database and starting again with an empty one.

So, it turns out the solution was similar to this one
Entity Framework code first migrations throwing error
When I first attempted this solution there were reports of gacutil not being recognised. Due to the environment I work it it is not simple to add the gacutil to be universally recognised (I believe), so i found the gacutil.exe in the file system, copied that to the lib\net45 folder referenced in the linked post, and then ran the command
gacutil /i EntityFramework.dll
This has resolved the problem, and i no longer receive the 'File not Found error'

Instead of installing with GAC, I came to the following solution (VS2013 SP5 + EF 5):
Copied (Solution dir)\packages\EntityFramework.5.0.0 (entire directory)
to
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\CommonExtensions\Microsoft\EFTools\NuGet Packages\
The NuGet Packages directory did not exist, so one needs to create it manually.

Related

Can't get migrations to work: Cannot bind argument to parameter 'Path' because it is null

I'm taking a course in MVC that was recorded in 2016, so maybe that has to do with this error. It says to open NuGet console and type in "Enable-Migrations" in order to enable Entity Framework. But when I type that I get all sorts of error messages and yes I double-checked to make sure I'm doing everything the instructor is. I tried different versions of EF in Nuget Package Manager and the error messages change but still happen.
PM> Enable-Migrations
Enable-Migrations : Cannot bind argument to parameter 'Path' because it is null.
At line:1 char:1
+ Enable-Migrations
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Enable-Migrations], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Enable-Migrations
First off, why is this even necessary? From what I've seen of EF and LINQ so far I'd rather code in machine language, but in courses I took before that used EF we never had to do "Migrations" before so I'd like to solve this error message but I don't even see the point of it. I thought I read somewhere that Microsoft in its infinite wisdom has changed the way we do migrations but can't find any updated documentation.
Type following command in package manager console.
dir
or
pwd
pwd show you the path of current directory.
dir list down all folders of current directory. if package manager console is not set to project directory, run
cd "YouProjectDirectory"
and run
Enable-Migrations
If you install required packages in this case entitty framework core packages, it will run successfully.

VS2017 .NetCore 2.0 API - Could not load file or assembly Microsoft.AspNetCore.Hosting.Abstractions

I've just created a new API using ASP.NET Core 2.0.
I haven't coded anything yet. I just want to test the initial API template.
When I try to start my project I receive the following error:
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in Unknown Module.
Could not load file or assembly 'Microsoft.AspNetCore.Hosting.Abstractions, Version=2.0.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
I do not receive any errors when building the project.
The assemby can be found at the following directory: 'C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.hosting.abstractions\2.0.2'.
Is that correct? Where should the 'Microsoft.AspNetCore.Hosting.Abstractions' assembly be located and why can't VS find it?
I had the same error attempting to debug a simple Azure Function App (this is using .NET core) created from the Function App template in Visual Studio 2017. Attempting to run in debugger gave me the same error. It turned out to be I had updated the Microsoft.NET.Sdk.Functions package from version 1.0.6 to 1.0.12. That was my problem. Reverted the update back to 1.0.6 and it worked fine. Hope this helps!
Please re-install the nuget package or upgrade to the latest at: https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.Abstractions/

Pro Asp.Net 2016 nuget Update-Database cmdlet error

I am new to Asp.Net Core. While reading Adam Freeman's book on Asp.Net Core (2016)
I have done everything properly prior to my problem, however, I can not create database tables using Update-Database Project manager console command as said in the book. Similar topics did not help me. Here is the error.
Invoke must be called on a background thread.
Update-Database : The term 'Update-Database' is not recognized as the name
of a cmdlet, function, script file, or operable program. Check the spelling
of the
name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1
+ Update-Database
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Update-Database:String) [],
Comman
dNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
What version of ASP.NET Core did you select when created the project?
Did you add both Microsoft.EntityFrameworkCore.SqlServer and Microsoft.EntityFrameworkCore.Tools packages, did you do that via the Nuget manager or by editing your .csproj and what versions of that packages did you add?
In the book he adds packages by editing project.json which is no more supported and is replaced by PROJECT_NAME.csproj.
Make sure your dependencies were added and there are no warning signs (In Solution Explorer, expand your project tree. Check if required packages are listed in Nuget section under the "Dependencies" and there is no yellow warning triangle sign on the item).

EF 6 with a dnx project

I have a new ASP.net 5 dnx class library I am using for entity framework. I need to target EF 6 because some features I need are not in EF 7.
First the EF tools (like enable-migration) were not there. I added an old style class library and installed EF 6 and now the commands are there.
When I run enable migrations I get this error:
PM> Enable-Migrations
Exception calling "SetData" with "2" argument(s): "Type
'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject'
in assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation,
Version=14.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is
not marked as serializable." At
D:\Projects\Rebus\FMS\Code\Current\FMSSupport\FMSSupport\packages\EntityFramework.6.1.3\tools\EntityFramework.psm1:720
char:5
+ $domain.SetData('startUpProject', $startUpProject)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SerializationException System.NullReferenceException: Object reference not set to an instance
of an object. at
System.Data.Entity.Migrations.Extensions.ProjectExtensions.GetProjectTypes(Project
project, Int32 shellVersion) at
System.Data.Entity.Migrations.Extensions.ProjectExtensions.IsWebProject(Project
project) at
System.Data.Entity.Migrations.MigrationsDomainCommand.GetFacade(String
configurationTypeName, Boolean useContextWorkingDirectory) at
System.Data.Entity.Migrations.EnableMigrationsCommand.FindContextToEnable(String
contextTypeName) at
System.Data.Entity.Migrations.EnableMigrationsCommand.<>c__DisplayClass2.<.ctor>b__0()
at
System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action
command) Object reference not set to an instance of an object.
I have tried different options for enable-migration such as specifying a startup project or the contextTypeName.
Any ideas?
Following this recommendation, EF6 data projects should use the existing .NET Framework class library csproj type.
Even after following the recommendations, I was still getting the same error. Unloading your ASP.NET Core project(s) and then invoking the Enable-Migrations command on the appropriate csproj should allow the command to execute. You can unload a project by right-clicking on the .NET Core project and choosing the 'Unload Project' option. It should also be noted that the Package Manager Console window has a 'Default project:' drop-down selection. You should choose the project with your DbContext.
It's because ASP.NET Core introduced a new project type/configuration system (see package.config vs project.json) and the old EF tools don't know how to work with the newer system (project.json). It would probably be easiest to create a new project and port your code into it if you want to go back to EF6.
Interestingly (well, disappointingly actually) they're backtracking on project.json see:
http://xoofx.com/blog/2016/05/11/goodbye-project-json/
I had the same issue. Just setting the start project from ASP.NET Core to my .NET Framework Lib does the trick for me =) Now i can do EF6 migrations as usual "Add-Migration" command...
P.S. Sorry for my eng.

Error on Enable-Migrations

I'm new with EF and I have an error when I try to run Enable-Migrations in PMC console :
Exception calling "LoadFrom" with "1" argument(s):
"Could not load file or assembly 'file:///...\pa
ckages\EntityFramework.6.0.1\tools\EntityFramework.PowerShell.Utility.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)"
At ...\packages\EntityFramework.6.1.1\tools\EntityFramework.psm1:780 char:62
+ $utilityAssembly = [System.Reflection.Assembly]::LoadFrom <<<< ((Join-Path $ToolsPath EntityFramework.PowerShell.Utility.dll))
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
You cannot call a method on a null-valued expression.
At ...\packages\EntityFramework.6.1.1\tools\EntityFramework.psm1:781 char:50
+ $dispatcher = $utilityAssembly.CreateInstance <<<< (
+ CategoryInfo : InvalidOperation: (CreateInstance:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Exception calling "CreateInstanceFrom" with "8" argument(s): "Could not load file or assembly 'file:///...\packages\EntityFramework.6.0.1\tools\EntityFramework.PowerShell.dll' or one of its dependencies. Operation is no t supported. (Exception from HRESULT: 0x80131515)"
At ...\packages\EntityFramework.6.1.1\tools\EntityFramework.psm1:809 char:31
+ $domain.CreateInstanceFrom <<<< (
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
I just ran into this on one of my co-worker's machine. The solution was:
Uninstall EF NuGet package
Manually remove Entity Framework sections from the Web.Config/App.Config files
Close Visual Studio
Re-open the solution in Visual Studio
Reinstall the EF NuGet Package
So openn the solution folder in file explorer, can you find the .dlls they are looking for in "Packages" folder then EntityFramework 6.0.1 folder? If not, there are two options:
Check your source control server, there is a possibility it was not downloaded to local workspace. if so, just get them manually from source control explorer if you are using TFS.
If the files were not on source control server either, I would recommend to uninstall EF 6.0.1 nuget, then manual delete the whole folder about ef in the Package folder above, and then reinstall it.
I had the same problem. My project was stored in a network drive. There are many security policies applied to my PC. I moved my project to the local hard drive, and the problem was solved.
It seems like "EntityFramework.Powershell.utility.dll" is tring to use some powershell functionality, and the system is preventing to execute such calls, maybe because the dll that is calling powershell is not a local file.
In your case, make sure that you do not have any security policy or system configuration that would prevent your system execute the problematical extension from a non local drive (EntityFramework.Powershell.utility.dll)
Faced similar problem. Reason of the problem was shifting of my code from one machine to another...Removing and then reinstalling entity framework solved the problem.
Update the Entity Package from Package Manager Console or browser.

Categories

Resources