AzureAD / azure-activedirectory-identitymodel-extensions-for-dotnet

IdentityModel extensions for .Net
MIT License
1.06k stars 401 forks source link

[Bug] EF Core Migration doesnt work any more after upgrading JsonWebTokens from 7.6.0. to 8.x #2854

Open ReneKochITTitans opened 1 month ago

ReneKochITTitans commented 1 month ago

Which version of Microsoft.IdentityModel are you using?

    <PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="7.6.0" />
    <PackageReference Include="Microsoft.IdentityModel.Logging" Version="7.6.0" />
    <PackageReference Include="Microsoft.IdentityModel.Protocols" Version="7.6.0" />
    <PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="7.6.0" />
    <PackageReference Include="Microsoft.IdentityModel.Tokens" Version="7.6.0" />

(In Bug case I updated to 8.0.2 / 8.1.0, both created the bug)

Where is the issue? see above

Is this a new or an existing app? App exists since a few years.

Repro When upgrading my nugets from 7.6.0 to 8.x, the EF Core migration command fails. It works, when I dont upgrade.

EF Command: dotnet ef migrations add Test (but also fails on e.g. dotnet ef migrations has-pending-model-changes)

DbContext:

` public class GenesisDbContext : DbContext { private readonly IHttpContextAccessor _httpContextAccessor;

    public GenesisDbContext(DbContextOptions<GenesisDbContext> dbContextOptions, IHttpContextAccessor httpContextAccessor) : base(dbContextOptions)
    {
        _httpContextAccessor = httpContextAccessor;
        AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
    }

// Just a few DbSets and seeding here

`

When I run with 7.6.0 the command dotnet ef migrations has-pending-model-changes I get: No changes have been made to the model since the last migration.

When I upgrade to 8.1.0 I Get: Unable to create a 'DbContext' of type ''. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[SMS.Server.Genesis.Data.GenesisDbContext]' while attempting to activate 'KommuneAktiv.SMS.Server.Genesis.Data.GenesisDbContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

Expected behavior Migration worked as before

Actual behavior Migration command throws error: Unable to create a 'DbContext' of type ''. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[SMS.Server.Genesis.Data.GenesisDbContext]' while attempting to activate 'KommuneAktiv.SMS.Server.Genesis.Data.GenesisDbContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

pmaytak commented 1 month ago

@ReneKochITTitans

Which version of the ef tool do you run? What versions of EntitityFramework related packages to do you reference?

Can you run the command with --verbose flag? Maybe it'll give a more descriptive error.