Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 619
PBG Avatar asked May 21 '13 20:05

PBG


People also ask

Why add migration is not working?

Add-Migration - The Term 'Add-Migration' Is Not Recognized After creating models and context class, we nomally add migration to initialize the database. The error occurs sometimes while adding migration in asp.net core, entity framework with code first approach because of some missing library.

How do I change migrations in assembly?

By default, the migrations assembly is the assembly containing the DbContext. Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project."

What does add migration command do?

Add-Migration: Creates a new migration class as per specified name with the Up() and Down() methods. Update-Database: Executes the last migration file created by the Add-Migration command and applies changes to the database schema.

How do I run an add migration in Visual Studio?

Adding a Migration So, firstly, you need to create a migration. Open the Package Manager Console from the menu Tools -> NuGet Package Manager -> Package Manager Console in Visual Studio and execute the following command to add a migration.


2 Answers

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

like image 176
PBG Avatar answered Sep 19 '22 07:09

PBG


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.)

like image 43
Dave Cousineau Avatar answered Sep 18 '22 07:09

Dave Cousineau