使用DB上下文工厂存储标识



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

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

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

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

验证服务描述符"ServiceType:MicrosoftAspNetCore.Identity.UserManager1[SelfProgress.Domain.User] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager1[SelfProgress.Domain.User]"时出错:尝试激活时无法解析类型为"SelfProgress.Orm.SelfProgress DbContext"的服务>'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.IdentityRoleClaim`1[System.Guid]]'。

验证服务描述符"ServiceType:Microsoft.AspNetCore.Identity.ISecurityStampValidator Lifetime:Scoped ImplementationType:Microsoft.阿斯pNetCorE.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 Identity Role1[System.Guid],SelfProgress.Orm.SelfProgressDbContext,System.Guid,Microsoft.AspNetCore.Identity.IdentityUserClaim1[System.Guid],Microsoft.Asp NetCore.Identity Identity UserRole1[System.Guid],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System-Guid]时出错,Microsoft.AspNetCore.IdentityUserToken1[System.Guid],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.Guid]]'.

验证服务描述符"ServiceType:MicrosoftAspNetCore.Identity。RoleManager1[Microsoft.AspNetCore.Identity.IdentityRole1[System.Guid]]生存期:作用域实现类型:Microsoft.AspNetCore.Identity.RoleManager1[Microsoft.AspNetCore.Identity.IdentityRole1[System.Guid]]"时出错:在尝试激活"Microsoft.AspNetCore.Identity.EntityFrameworkCore.RoleStore"时,无法解析类型为"SelfProgress.Orm.SelfProgressDbContext"的服务5[Microsoft.AspNetCore.Identity.IdentityRole1[System.Guid],SelfProgress.Orm.SelfProgressDbContext,System.Guid,Microsoft.AspNetCore.IdentityUserRole1[System.Guid],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.Guid]]'.

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

我的身份注册:

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

有没有一种方法可以让默认的Identity存储通过其新工厂解析DB上下文?

在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
{
...
}

最新更新