如何解决带有 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.UserManager
1[SelfProgress.Domain.User]”时出错:尝试解析时无法解析“SelfProgress.Orm.SelfProgressDbContext”类型的服务激活 >
'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.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.UserStore
9[SelfProgress.Domain.User,Microsoft.AspNetCore.Identity。 IdentityRole1[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.RoleManager1[Microsoft.AspNetCore.Identity.IdentityRole
1[System.Guid]] Lifetime:Scoped ImplementationType:Microsoft.AspNetCore.Identity.RoleManager1[Microsoft.AspNetCore.Identity.IdentityRole
1[ System.Guid]]”:尝试激活“Microsoft.AspNetCore.Identity.EntityFrameworkCore.RoleStore5[Microsoft.AspNetCore.Identity.IdentityRole
1[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
{
...
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。