dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.63k stars 3.15k forks source link

Migrate function fails silently if Microsoft.EntityFrameworkCore.Relational package is not installed. #25982

Open DizzyDeveloper opened 2 years ago

DizzyDeveloper commented 2 years ago

Not sure where, if this is an error, belongs. So I created it here.

The context.Database.Migrate(); method appears to fail silently if the project that defines the context and corresponding entities does not have the Microsoft.EntityFrameworkCore.Relational nuget package installed. The database does get created, but none of the migrations get applied.

However, if I run the Update-Database call via the Package Manager Console the database gets created and the migration gets applied correctly.

Initially, I thought it was an issue with MigrationAssembly not being correct for some reason, so after explicitly declaring the MigrationAssembly in the OnConfiguring method I was suddenly being presented with the following error (when before there was no errors being presented.)

Could not load file or assembly 'Microsoft.EntityFrameworkCore.Relational, Version=5.0.9.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

Noticing that I was missing this nuget package in the context project, I added in said package and removed the explicit MigrationAssembly declaration. Then the migrations were being applied correctly.

Include provider and version information

EF Core version: 5.0.9 Database provider: Npgsql.EntityFrameworkCore.PostgreSQL Target framework: .NET 5.0 Operating system: Windows 10 x64 IDE: Visual Studio 2019 16.3

roji commented 2 years ago

@DizzyDeveloper can you submit a full code sample for the above?

DizzyDeveloper commented 2 years ago

Sample.zip

Hi @roji,

I have attached a small simplistic sample project that should show what I mentioned above,. Unzipped the Domain project does not have the Microsoft.EntityFrameworkCore.Relational package installed. So if you run the Create_And_Migrate_Database test in the Test project you will see the silent failure. So it will create the database, but not apply any of the migrations. If you then add the Microsoft.EntityFrameworkCore.Relational package (5.0.9) to the Domain project and run the test again you should see the database get created and migrations applied.

Just need to supply your own connection string.

roji commented 2 years ago

I can repro this, thanks - after Migrate completes, there's the history migration table but no actual migrations are applied.

Building this project produces the following error:

/home/roji/.dotnet/sdk/6.0.100-rc.1.21379.2/Microsoft.Common.CurrentVersion.targets(2205,5): warning MSB3277: There was a conflict between "Microsoft.EntityFrameworkCore.Relational, Version=5.0.7.0, Culture=neutral, PublicKeyToken=adb9793829ddae60" and "Microsoft.EntityFrameworkCore.Relational, Version=5.0.9.0, Culture=neutral, PublicKeyToken=adb9793829ddae60". [/home/roji/repros/Sample/tests/Sample.Domain.Tests/Sample.Domain.Tests.csproj]
/home/roji/.dotnet/sdk/6.0.100-rc.1.21379.2/Microsoft.Common.CurrentVersion.targets(2205,5): warning MSB3277:     "Microsoft.EntityFrameworkCore.Relational, Version=5.0.7.0, Culture=neutral, PublicKeyToken=adb9793829ddae60" was chosen because it was primary and "Microsoft.EntityFrameworkCore.Relational, Version=5.0.9.0, Culture=neutral, PublicKeyToken=adb9793829ddae60" was not. [/home/roji/repros/Sample/tests/Sample.Domain.Tests/Sample.Domain.Tests.csproj]
/home/roji/.dotnet/sdk/6.0.100-rc.1.21379.2/Microsoft.Common.CurrentVersion.targets(2205,5): warning MSB3277:     References which depend on "Microsoft.EntityFrameworkCore.Relational, Version=5.0.7.0, Culture=neutral, PublicKeyToken=adb9793829ddae60" [/home/roji/.nuget/packages/microsoft.entityframeworkcore.relational/5.0.7/lib/netstandard2.1/Microsoft.EntityFrameworkCore.Relational.dll]. [/home/roji/repros/Sample/tests/Sample.Domain.Tests/Sample.Domain.Tests.csproj]
/home/roji/.dotnet/sdk/6.0.100-rc.1.21379.2/Microsoft.Common.CurrentVersion.targets(2205,5): warning MSB3277:         /home/roji/.nuget/packages/microsoft.entityframeworkcore.relational/5.0.7/lib/netstandard2.1/Microsoft.EntityFrameworkCore.Relational.dll [/home/roji/repros/Sample/tests/Sample.Domain.Tests/Sample.Domain.Tests.csproj]
/home/roji/.dotnet/sdk/6.0.100-rc.1.21379.2/Microsoft.Common.CurrentVersion.targets(2205,5): warning MSB3277:           Project file item includes which caused reference "/home/roji/.nuget/packages/microsoft.entityframeworkcore.relational/5.0.7/lib/netstandard2.1/Microsoft.EntityFrameworkCore.Relational.dll". [/home/roji/repros/Sample/tests/Sample.Domain.Tests/Sample.Domain.Tests.csproj]
/home/roji/.dotnet/sdk/6.0.100-rc.1.21379.2/Microsoft.Common.CurrentVersion.targets(2205,5): warning MSB3277:             /home/roji/.nuget/packages/microsoft.entityframeworkcore.relational/5.0.7/lib/netstandard2.1/Microsoft.EntityFrameworkCore.Relational.dll [/home/roji/repros/Sample/tests/Sample.Domain.Tests/Sample.Domain.Tests.csproj]
/home/roji/.dotnet/sdk/6.0.100-rc.1.21379.2/Microsoft.Common.CurrentVersion.targets(2205,5): warning MSB3277:     References which depend on "Microsoft.EntityFrameworkCore.Relational, Version=5.0.9.0, Culture=neutral, PublicKeyToken=adb9793829ddae60" []. [/home/roji/repros/Sample/tests/Sample.Domain.Tests/Sample.Domain.Tests.csproj]
/home/roji/.dotnet/sdk/6.0.100-rc.1.21379.2/Microsoft.Common.CurrentVersion.targets(2205,5): warning MSB3277:         /home/roji/repros/Sample/src/Sample.Domain/bin/Debug/net5.0/Sample.Domain.dll [/home/roji/repros/Sample/tests/Sample.Domain.Tests/Sample.Domain.Tests.csproj]
/home/roji/.dotnet/sdk/6.0.100-rc.1.21379.2/Microsoft.Common.CurrentVersion.targets(2205,5): warning MSB3277:           Project file item includes which caused reference "/home/roji/repros/Sample/src/Sample.Domain/bin/Debug/net5.0/Sample.Domain.dll". [/home/roji/repros/Sample/tests/Sample.Domain.Tests/Sample.Domain.Tests.csproj]
/home/roji/.dotnet/sdk/6.0.100-rc.1.21379.2/Microsoft.Common.CurrentVersion.targets(2205,5): warning MSB3277:             /home/roji/repros/Sample/src/Sample.Domain/bin/Debug/net5.0/Sample.Domain.dll [/home/roji/repros/Sample/tests/Sample.Domain.Tests/Sample.Domain.Tests.csproj]

This is because Microsoft.EntityFrameworkCore 5.0.9 is being used with Npgsql.EntityFrameworkCore.PostgreSQL 5.0.7. Either downgrading to Microsoft.EntityFrameworkCore 5.0.7 in the project or explicitly referencing Microsoft.EntityFrameworkCore.Relational 5.0.9 makes the problem go away.

DizzyDeveloper commented 2 years ago

Hi @roji, Thanks for that :)

However, sorry there is an however, I probably more concerned about the symptom rather than the cause. As mentioned above I was also able to resolve it by just including the package.

There does not appear to be any indication that this is a build issue. Just an odd silent "failure" in the Migrate call. Considering that there are no build errors, I am able to generate migrations to my hearts content, I can even successfully apply the migrations manually using the Update-Database call, and against a pre-existing DB I can happily run EF queries.

The only way that I was able to detect that there was a build related issue was when I added the opt.MigrationAssembly ("..."), that then cause the Migrate to throw that error that I mentioned above about the missing assembly? Shouldn't the Migrate function be indicating that "hey something is a miss here" even without opt.MigrationAssembly being needed. Caveat I don't know if it is actually that function or that function triggers some other validation that can happen without that needing to be specified.

Those were just my final thoughts, I suspect this is probably a reasonably niche issue. So if you want to close this you can.

roji commented 2 years ago

@DizzyDeveloper we agree that there's an issue here (silent failure), and that we need to investigate; but as you say, this isn't the highest-priority problem, so it is currently in the backlog.