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

Viewed 4435

There is a "Data" project containing the context of app, and the root project "TheFarmersMarketApp" with reference to Data project. The main project is written on WPF.

Add-Migration "Initial Migration" -Project Data -StartUpProject TheFarmersMarketApp

returns

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

Additionally, I changed "Default project" field on "Data" and "Models"(ugh...) projects and restarted my laptop to be sure that all necessary services are turned on.

There are some screenshots of Visual Studio, if it is easier to understand the problem visually.

PackageManagerConsole + SolutionExplorer + TheFarmersMarket.csproj windows

Output after rebuilding + ErrorList windows.

4 Answers

I encountered such a problem. After hours of fixing it, I found that the problem is the Package Manager Console or Visual Studio CLI.

I used Windows Command Prompt and dotnet tool

> dotnet ef migrations add migration_name

After executing the command, it successes without any error with the message:

> Done. To undo this action, use 'ef migrations remove
Could not load assembly 'Data'. Ensure it is referenced by the startup project 'TheFarmersMarketApp'.

Please make sure that you add the Data assembly into the project TheFarmersMarketApp. Please make sure that all projects that need to reference this project have a direct reference to it, as long as it does not cause a circular dependency or other issues.

First of all you need to select a proper project in your Nuget Package Manager Console. As you can see on your first screen the default project is TheFarmersMarketApp, but it is not right. You need to select the Data project, because your migrations must lay near your context class.

Then you should use a short version of your command:

Add-Migration "Initial Migration" 

Had this issue due to switching my project's target to x86 in order for it to run with an older library.

My Fix: Right click on your project then select Properties - Build - General - and set platform Target to x64, run your command then switch back to x86 if necessary for your project to run.

Related