带有 DB Context Factory 的身份存储

问题描述

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

services.AddDbContext<SelfProgressDbContext>(...);

services.AddPooledDbContextFactory<SelfProgressDbContext>(...);

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

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

验证服务描述符时出错“ServiceType: Microsoft.AspNetCore.Identity.ISecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.SecurityStampValidator1[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。 IdentityRole1[System.Guid],Microsoft.AspNetCore.Identity.IdentityUserClaim1[System.Guid],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.Guid],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.Guid],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.Guid],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.Guid] ]'。

验证服务描述符时出错“ServiceType:Microsoft.AspNetCore.Identity.RoleManager1[Microsoft.AspNetCore.Identity.IdentityRole1[System.Guid]] Lifetime:Scoped ImplementationType:Microsoft.AspNetCore.Identity.RoleManager1[Microsoft.AspNetCore.Identity.IdentityRole1[ System.Guid]]”:尝试激活“Microsoft.AspNetCore.Identity.EntityFrameworkCore.RoleStore5[Microsoft.AspNetCore.Identity.IdentityRole1[System.Guid],SelfProgress.Orm”时,无法解析“SelfProgress.Orm.SelfProgressDbContext”类型的服务。 SelfProgressDbContext,Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.Guid]]'。

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

我的身份注册:

services
    .AddIdentity<User,IdentityRole<Guid>>(...)
    .AddEntityFrameworkStores<SelfProgressDbContext>()
    .AddDefaultTokenProviders()

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

解决方法

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

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

        services.AddScoped<ApplicationDbContext>(p => p.GetRequiredService<IDbContextFactory<ApplicationDbContext>>().CreateDbContext());

ApplicationDbContext.cs:

public class ApplicationDbContext : IdentityDbContext
{
     ...
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...