How to Update-Database in PM console when getting "Could not load assembly..." error?

Filip Veselý 20 Reputation points
2024-09-13T13:56:24.9266667+00:00

Hello,

I cannot apply migrations to database by Package Manager Console command Update-Database. On every attempt I get error:

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

Before mentioned error the application worked for 5 years, but I had issues with database, so I decided to update SW: I replaced old SQL Server 2019 to the new SQL Server 2022. Installed new Visual Studio 2022. Updated all installed NuGet packages.

I read possible solutions on the internet and tried apply them. Here are some of them:

  1. Solution: At the top of the "Package Manager Console" default project could be set to the wrong project. Changing that to my models project fixed it. Result: Did not help.
  2. Solution: This can also be caused by a platform mismatch between .NET Core and your project. 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. Result: I tried to switch to different platform target in project properties, but this field has bug. When I switch to different platform, it lasts only 1s and jumps back to the previous option. It is probably just GUI bug, because in .csproj file the platform selection changed. So lets assume it did the job and changed platform. After that I built project and tried Update-Database and again got same error. I tried it for x84, x64 and even AnyCPU.
  3. Solution: 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. Result: When I change the order of paths and move "C:\Program Files (x86)\dotnet" above "C:\Program Files\dotnet" and restart Visual Studio, it doesnt load project at all.

In SQL Server Object Explorer I can normally connect to the database and see the content.. Could you help me to launch the application again?

Thank you in advance

Filip VESELÝ

Installed products:

Microsoft Visual Studio Community 2022 Version 17.11.3

VisualStudio.17.Release/17.11.3+35303.130

Microsoft .NET Framework Version 4.8.09037

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
742 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,560 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,827 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 54,401 Reputation points
    2024-09-15T22:46:38.5266667+00:00

    Let's try to break this down for you. Firstly each project has a configuration (e.g. debug or release) and a platform (e.g. any cpu, x86, x64). The configuration generally controls compiler settings like debugging and optimizations. The platform determines what it runs on. An x64 project cannot run on an x86 OS, for example. For .NET projects it is generally recommended you target Any CPU so the platform is determined at runtime based on the OS it is running on. Every project has its own configuration/platform combinations and you can create custom ones.

    When you build your solution then VS has to build the configuration/platform for all the projects in it. The solution has its own configuration/platform combinations that you can customize. This is what you're selecting when you use the toolbar to select configuration/platform. In general this maps to the same configuration/platform for each project. But there may be cases where a project doesn't support those combinations, for example when a project is x64 only. This is where the configuration manager UX comes in. You can specify what projects should be built and what configuration/platform for that project should be used for each combination of solution configuration/platforms. In general you should leave it alone.

    If you have a project that only supports x86 then you should probably just go ahead and configure your project to only support that platform. Then use configuration manager to update all the solution configuration/platforms to use that same platform for your project. It doesn't make sense to have an x64 platform in this case so I would recommend you remove it from the solution and then from any projects that may have it (they shouldn't). But wait a second...

    If you have a console/windows app then leave the project as Any CPU. In the project properties settings there is an option to prefer 32-bit. This is compatibility flag that basically has your app build as Any CPU (preferred) but it runs as an x86 app. This is what that setting was designed for. But there isn't an option for that with a web app. The bitness of a web app is determined by the app pool it runs in. So in this case your best bet is to set the platform to x86 explicitly.

    Now, why did it work before and it doesn't now. VS 2019 was x86 based so when you ran the embedded Package Manager Console it was running x86, I'd wager. VS 2022 is x64 so it is running the x64 version instead. When the Update-Dabase command runs then it will use the startup project's runtime (whatever you defined in VS). So set your startup project in VS to be your web app and set the solution platform to be Any CPU. The configuration should be Debug.

    Summary:

    • In Configuration Manager you should have the following solution configurations
      • Debug/Any CPU
        • Web project - Debug/x86
        • Other projects - Debug/Any CPU
      • Release/Any CPU
        • Web project - Release/x86
        • Other projects - Release/Any CPU
    • Ensure your web project's platform is set to x86
    • Ensure the active solution is set to Debug/Any CPU in the toolbar
    • Build your solution and make sure it works
    • Now run the Update-Database command and verify it is using the correct path
    • Run the code in the debugger and it should work correctly.
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Michael Taylor 54,401 Reputation points
    2024-09-14T15:59:24.26+00:00

    If you look in the info from Update-Database notice that it is referencing the x64 Release build of your project. The first thing we want to do is make sure your project is building what you think it is.

    In Solution Explorer click on the console project file to open it in the editor. The OutputType should be Exe. The TargetFramework should be net8.0. There should be no references to any platforms here. If there are then remove them. The cleanest possible console NET 8 project would look like this.

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
      </PropertyGroup>
    </Project>
    

    Now you need to build a Release x64 version of your app. This is most likely in your toolbar somewhere but just in case, open Configuration Manager and it will show you the active solution configuration and platform. Set the configuration to Release and the platform to x64. Ensure that your project is listed as being for the same configuration. Then rebuild your solution and try the update again.

    1 person found this answer helpful.
    0 comments No comments

  2. Michael Taylor 54,401 Reputation points
    2024-09-13T14:04:10.6766667+00:00

    Does your code compile? If it doesn't then there is no assembly to load. Does your code start properly, minus the database migrations, when you try to debug it?

    What version of .NET are you targeting?

    Can you share the exact command you're running along with the actual error message that you're getting back?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.