'Error adding migration when data models are in a separate assembly
I've just setup new projects, Pegasus (my main web application) and PegasusEntities (my database objects).
My Pegasus start-up code includes the following.
builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlServer(
builder.Configuration.GetConnectionString(connectionString),
builder => builder.MigrationsAssembly("PegasusEntities"));
});
If I set Pegasus as my default project in Package Manager Console and try to add a migration, I get an error.
PM> add-migration CreateDatabase
Multiple startup projects set.
Using project 'Pegasus' as the startup project.
Build started...
Build succeeded.
Your target project 'Pegasus' doesn't match your migrations assembly 'PegasusEntities'. Either change your target project or change your migrations assembly.
Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("Pegasus")). 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.
I don't understand the problem. In what way do the projects "not match?" And why is it asking me to set the migrations assembly to Pegasus? I'm doing the same thing in another project and it's been working fine.
Can someone explain why this isn't working?
Update
Although I'm doing the same thing in another project and it works fine, I tried setting the default project in Package Manager Console to PegasusEntities. When I do, I get a completely different error.
PM> add-migration CreateDatabase
Multiple startup projects set.
Using project 'PegasusEntities' as the startup project.
Build started...
Build succeeded.
It was not possible to find any compatible framework version
The framework 'Microsoft.NETCore.App', version '2.0.0' (x64) was not found.
- The following frameworks were found:
5.0.12 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
5.0.16 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
6.0.4 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
You can resolve the problem by installing the specified framework and/or SDK.
The specified framework can be found at:
- https://aka.ms/dotnet-core-applaunch?framework=Microsoft.NETCore.App&framework_version=2.0.0&arch=x64&rid=win10-x64
This makes no sense to me. All projects are targeting net6.0
. Why is it looking for version 2 of Microsoft.NETCore.App
? When I open the suggested link, I get a page that says This release has reached end of life, meaning it is no longer supported. We recommend moving to a supported release, such as 7.0 Runtime..
Solution 1:[1]
I had this same issue only yesterday.
The error message is very misleading, if not just plain incorrect.
The fix is to set your debug start up project to the one that instantiates the DbContext whilst keeping the project with the entities selected in the package manager console.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Kieran Foot |