带有数据库上下文工厂的身份存储

Vla*_*hub 7 entity-framework-core asp.net5 asp.net-core-identity .net-5 asp.net-core-5.0

在我的 ASP.NET Core 5.0 项目中,我改变了

services.AddDbContext<SelfProgressDbContext>(...);
Run Code Online (Sandbox Code Playgroud)

services.AddPooledDbContextFactory<SelfProgressDbContext>(...);
Run Code Online (Sandbox Code Playgroud)

现在应用程序没有启动。我得到的错误的子集:

验证服务描述符“ServiceType:Microsoft.AspNetCore.Identity.UserManager 1[SelfProgress.Domain.User] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager1[SelfProgress.Domain.User]”时出错:尝试激活>“Microsoft.AspNetCore.Identity”时无法解析类型“SelfProgress.Orm.SelfProgressDbContext”的服务.EntityFrameworkCore.UserStore 9[SelfProgress.Domain.User,Microsoft.AspNetCore.Identity.IdentityRole1[System.Guid]、SelfProgress.Orm.SelfProgressDbContext、System.Guid、Microsoft.AspNetCore.Identity.IdentityUserClaim 1[System.Guid],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.Guid]、Microsoft.AspNetCore.Identity.IdentityUserLogin 1[System.Guid],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.Guid]、Microsoft .AspNetCore.Identity.IdentityRoleClaim`1[System.Guid]]'。

验证服务描述符'ServiceType:Microsoft.AspNetCore.Identity.ISecurityStampValidator Lifetime:Scoped ImplementType:Microsoft.AspNetCore.Identity.SecurityStampValidator 1[SelfProgress.Domain.User]': Unable to resolve service for type 'SelfProgress.Orm.SelfProgressDbContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[SelfProgress.Domain.User,Microsoft.AspNetCore.Identity.IdentityRole 1[System.Guid],SelfProgress.Orm.SelfProgressDbContext,System.Guid,Microsoft.AspNetCore.Identity.IdentityUserClaim1[System.Guid]时出错, Microsoft.AspNetCore.Identity.IdentityUserRole 1[System.Guid],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.Guid],Microsoft.AspNetCore.Identity.IdentityUserToken 1[System.Guid],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.Guid]]'。

验证服务描述符“ServiceType:Microsoft.AspNetCore.Identity.RoleManager 1[Microsoft.AspNetCore.Identity.IdentityRole1 [System.Guid]]生命周期时出错:作用域实现类型:Microsoft.AspNetCore.Identity.RoleManager 1[Microsoft.AspNetCore.Identity.IdentityRole1 [System.Guid]]”:无法解析类型的服务尝试激活“Microsoft.AspNetCore.Identity.EntityFrameworkCore.RoleStore 5[Microsoft.AspNetCore.Identity.IdentityRole1[System.Guid]、SelfProgress.Orm.SelfProgressDbContext、System.Guid、Microsoft.AspNetCore.Identity.IdentityUserRole 1[System.Guid],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.Guid”时出现“SelfProgress.Orm.SelfProgressDbContext” ]]'。

由于新的池数据库上下文工厂注册,Identity EF 存储类似乎无法再解析数据库上下文。使用AddPooledDbContextFactory似乎您无法再直接解析数据库上下文。相反,您应该解析工厂,然后手动创建数据库上下文。

我的身份注册:

services
    .AddIdentity<User, IdentityRole<Guid>>(...)
    .AddEntityFrameworkStores<SelfProgressDbContext>()
    .AddDefaultTokenProviders()
Run Code Online (Sandbox Code Playgroud)

有没有办法让默认身份存储通过其新工厂解析数据库上下文?

Zhi*_* Lv 2

在ConfigureServices中,尝试使用AddScoped()方法来注册DBContext并使用提供程序从服务中获取工厂。然后,将实例返回给提供者。代码是这样的:

        services.AddDbContextFactory<ApplicationDbContext>(options =>
        {
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            options.EnableSensitiveDataLogging();
        });

        services.AddScoped<ApplicationDbContext>(p => p.GetRequiredService<IDbContextFactory<ApplicationDbContext>>().CreateDbContext());
Run Code Online (Sandbox Code Playgroud)

ApplicationDbContext.cs:

public class ApplicationDbContext : IdentityDbContext
{
     ...
}
Run Code Online (Sandbox Code Playgroud)