121

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.

3
  • 2
    How can Registry depend on Data and Data be the starup project? Commented May 21, 2013 at 20:25
  • 1
    Registry is my main project, data just contains all my model classes. It doesn't matter which startup project I choose, i get an error in both cases.
    – PBG
    Commented May 21, 2013 at 20:32
  • 2
    Googler here. +1 for mentioning the start-up project. It matters. Commented Jul 8, 2015 at 17:49

16 Answers 16

358

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

6
  • 7
    Same here. Even though I initially thought changing the startup project from Solution Explorer would suffice, this answer did the actual trick. Thanks! Commented Jul 9, 2017 at 20:35
  • 4
    Still valid for EF Core migrations as well!
    – Ziad Akiki
    Commented Jun 26, 2019 at 12:42
  • 3
    I guess this shouldnt say Default Project, it should just say Project.
    – Andrew
    Commented Oct 19, 2019 at 15:18
  • 2
    Came from our lord and savior Google the almighty, it turns out that it's not enough to set the project from the Package Manager Console, it should be as your startup project too! I don't see how is that make any sense.
    – Rickless
    Commented Jul 13, 2020 at 15:46
  • 1
    In one of my solutions "default project" option is ignored and VS keeps asking me to include DB migration project into Host (startup project). Using -project parameter does not help either. No any incompatible versions noticed. Nonsense...
    – Alexander
    Commented Oct 2, 2020 at 21:24
25

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

23

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

1
  • when I set the .net core project to startup project it was solved the issue Commented Jul 27, 2020 at 9:06
5

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

4

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.

1

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)

1

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

1

The first make sure about your startup project is API and after with CTRL + SHIFT + B rebuild your project and if don't solve your problem, close and open again your Visual Studio

0

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.

0

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.

0

This can also be caused by a platform mismatch between .NET Core and Also check this in your packages if don't have that package Dependency Injection Its very simple you have to install dependency injection package

0

The package manager window has a Default Project Property.

Setting the default project in the package manager window fixed this for me.

I also had to set the startup project to the same application in the solution explorer.

0

You should define 2 constructor and OnConfiguring method in context.cs. And also in your UI layer you should define connection string.

Like this:

In Context.cs :

    public Context()
      {
      }
    
      public Context(DbContextOptions<Context> options) : base(options)
      {
      }
    protected override void OnConfiguring(DbContextOptionsBuilder 
   optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer("Data Source=..");
            }
        }

In appsetting.json:

"ConnectionStrings": {
    "SqlServer": "Data Source=....."
  }

In Program.cs:

builder.Services.AddDbContext<Context>(opt =>
{
    opt.UseSqlServer(builder.Configuration.GetConnectionString("SqlServer"));
});
1
  • This has nothing to do with being able to load assemblies. Commented Dec 21, 2022 at 8:07
0

If you have multiple project within one solution, then you have to make that particular project as a startup project. Process: Right click on the project name -> set as start up project.

And also select the 'default project' from the dropdown in the Project Manager Console.

0

if both building the project and changing the default project didn't work and you are sure that the issue is not in platform

just remove the project reference and re-add it and build again maybe that can help

-2

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.