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

9.4k Views Asked by At

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.

7

There are 7 best solutions below

0
On

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" 
0
On
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.

1
On

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
0
On

I had the same error, it turns out that McAfee LifeSave was throwing notifications about a virus blocked every time I was trying dotnet EF database update.

When inspecting the files about the "virus" I noticed that it was concerning my project files.

I uninstalled McAfee LifeSave and everything worked perfectly fine.

0
On

This happened to me because I have had multiple projects. And my startup project was not the same as the database context project.

To solve this, you either need to change the startup project to the one with the database context OR explicitly call it via the command line.

Add-Migration "InitMigration" -Context AppDbContext -StartupProject myprojectthathasdbcontext -Verbose

The key point is StartupProject. I also use -Context to explicitly mention database context class and -Verbose, to get more debugging info.

0
On

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.

0
On

This is the problem.

enter image description here

As you can see my Scaffold-DbContext command has all the information it needs - including pointing the StartupProject to MyProject.Data

...

Not good enough!

The answer is in those two dots at the top right

enter image description here

As you can see, my DEFAULT project was MyProject.Api.Net2 rather than MyProject.Data