4

The command "Add-Migration InitialCreate" generates the following error message:

Could not load assembly 'MathModelApp'. Ensure it is referenced by the startup project 'MathModelApp'.

enter image description here

What does the error message mean?

I am using netcore 2.1, Target Platform is x86.

dergroncki
  • 798
  • 1
  • 12
  • 24
  • Usually this error means that you are effecting the wrong project in your solution or your project is not the startup project in your solution. – Llazar Nov 28 '18 at 17:46
  • From the screenshot you see that everything is setup correctly. The project compiles and runs without any problems. Only the migration of the EF fails. – dergroncki Nov 28 '18 at 18:01
  • I see you have a solution and inside it the project with the same name and a folder `Solution items`. Have you tried the migration from cmd with `dotnet ef add migration`? – Llazar Nov 28 '18 at 18:10
  • 7
    I switched the Target Platform from x86 to x64 and migration is working now. – dergroncki Nov 29 '18 at 11:25
  • 2
    Just for anyone searching for this issue, switching the target platform to Any CPU worked for me. I'd set it to x86 earlier to try and solve an issue with edit and continue. This x86 setting then stopped the EF tool from working. – vinnyh Mar 03 '20 at 15:40
  • switching to Any CPU works for me, but I'm really getting tired of constantly having to switch my configuration back and forth all the time for no good reason – Dave Cousineau Jun 19 '20 at 16:11
  • as per [this Q/A](https://stackoverflow.com/questions/39499882/start-an-asp-net-core-project-as-x86-under-visual-studio-2015) the solution is to modify your system PATH variable so that the x86 version of dotnet comes before the x64 version. – Dave Cousineau Jun 19 '20 at 16:28
  • See also: https://stackoverflow.com/questions/16678724/add-migration-causing-a-could-not-load-assembly-error?noredirect=1&lq=1 – Robin Bennett Jan 31 '23 at 08:44

4 Answers4

2

I solved it by changing the Start Project in the context menu, clicking with the right mouse button

enter image description here

César Augusto
  • 593
  • 1
  • 7
  • 7
1

Please go to solution property and check if you selected same project as the startup project in the solution or not.

1
  1. Right Click on EndPoint Project
  2. Click on the Set As Startup Project
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
0

I worked around this by using the .NET Core CLI instead of the Package Manager Console. Install the tools with the instructions here, then run dotnet ef migrations add MigrationName from the project directory.

Rico
  • 313
  • 2
  • 9