add-migration causing a "Could not load assembly" error

Entity FrameworkEntity Framework-Migrations

Entity Framework Problem Overview


Here's what I am looking at

PM> Add-Migration AddedSubdivion -StartUpProjectName Data -Verbose
Using StartUp project 'Data'.
Using NuGet project 'Registry'.
Could not load assembly 'Registry'. (If you are using Code First Migrations inside 
Visual Studio this can happen if the startUp project for your solution does not 
reference the project that contains your migrations. You can either change the startUp 
project for your solution or use the -StartUpProjectName parameter.)

I have no idea why it's trying to reference the Registry project. Registry depends on Data, not the other way around. I am very new to this, so I'd appreciate any help.

Entity Framework Solutions


Solution 1 - Entity Framework

This is embarrassing, but maybe this will help out a googler in the future.

At the top of the "Package Manager Console" my default project was set to the wrong project. Changing that to my models project fixed it.

enter image description here

Solution 2 - Entity Framework

The problem might not be so obvious if you have Package Manager Console docked with a narrow window...hiding the default project. enter image description here

Its needs a wide docking. enter image description here

Solution 3 - Entity Framework

This can also be caused by a platform mismatch between .NET Core and your project. You get the error:

> Could not load assembly 'DataProject'. Ensure it is referenced by the startup project 'ProgramProject'.

even though you have specified correct project and startup project names. (Either by using the drop down boxes in VS and the Package Manager Console, or by using the -project and -startupproject parameters.)

You can fix it by switching to Any CPU instead of x86, or vice-versa (or maybe to x64, etc.), but then you will have to switch back and forth every time you need to make changes to your model/DB.

As per this answer you can fix this by changing the order of your .NET Core path entries in system environment variables. If you're getting this error, then it means that either the first .NET Core path is for x64 but you're trying to make changes to your x86 project, or possibly other way around. Move the one you're targeting above the one you're not targeting, save, and then restart Visual Studio.

You can see which one is currently being used with the command dotnet --info.

(Note that this assumes you've installed both. You may also only have one of them installed, in which case you'd need to install the other one, and then check the order of the PATH entries; if the second one you installed is the one you want, then you will definitely need to change the PATH order to make it the one used by VS, since its entry should be at the bottom.)

Solution 4 - Entity Framework

If all fails there's always the verbose flag (-v).

A command like dotnet ef database update -v should help clarify the problem ef is facing.

In my case, the issue stemmed from EntityFramework finding it difficult to deal with the fact that my platform target was changed from AnyCPU to x86 or x64.

System.BadImageFormatException: Could not load file or assembly 'MyProject, Culture=neutral, PublicKeyToken=null'. An attempt was made to load a program with an incorrect format.

Temporarily changing the platform target to AnyCPU worked just fine.

Solution 5 - Entity Framework

Make sure that you are focused on:

1- Startup Projects is UI (MVC, API, ... etc).

2- Default project in package manager console is place of (ApplicationDbContext).

Solution 6 - Entity Framework

I had this exact problem, and it turned out because I createdthe project under a blank solution and then added the class libraies and web app seperately it didnt have a start up project.

Solution 7 - Entity Framework

I would like to add, if you are using .net6 preview, you will need to update the packages.

so you will need to use the preview versions EntityFrameworkCore.Tools and EntityFrameworkCore.SqlServer (6.0.0-rc-1.21452.10 version as of today)

Solution 8 - Entity Framework

None of these worked for me. I temporarily unloaded the unrelated project from the solution, ran the command, and then loaded the project back in.

Solution 9 - Entity Framework

I've got this issue migrating an Asp.Net 5 to Asp.Net 6.

The problema was in the .csproj file.

This configuration

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <PlatformTarget>x86</PlatformTarget>
</PropertyGroup>

I've just removed this property group and the dotnet ef worked

Solution 10 - Entity Framework

Set the target project for the migration as the startup project and continue

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPBGView Question on Stackoverflow
Solution 1 - Entity FrameworkPBGView Answer on Stackoverflow
Solution 2 - Entity FrameworkChris CatignaniView Answer on Stackoverflow
Solution 3 - Entity FrameworkDave CousineauView Answer on Stackoverflow
Solution 4 - Entity FrameworkPrince OwenView Answer on Stackoverflow
Solution 5 - Entity FrameworkAhmad HamdeenView Answer on Stackoverflow
Solution 6 - Entity FrameworkRichard BakerView Answer on Stackoverflow
Solution 7 - Entity FrameworkMidz ElwekilView Answer on Stackoverflow
Solution 8 - Entity FrameworkRidicCoderView Answer on Stackoverflow
Solution 9 - Entity FrameworkMurilo Maciel CurtiView Answer on Stackoverflow
Solution 10 - Entity FrameworksnnproView Answer on Stackoverflow