【发布时间】:2020-12-17 18:00:15
【问题描述】:
我有两个数据库。
- application_db
- registry_db
application_db 用于应用程序获得工作并使用 ApplicationDbContext。
registry_db 用于管理所有帐户。我想首先在我的注册表服务上创建一个帐户,然后将创建的帐户(信息较少)插入 application db。 Registry_db 正在使用 RegistryDbContext。
我已经通过依赖注入成功地在我的注册表服务中注入了两个上下文(ApplicationDbContext 和 RegistryDbContext)。
我的启动如下所示:
services.AddCors();
services
.AddDbContext<RegistryDbContext>(
options => options
.UseNpgsql(
Configuration.GetConnectionString("DefaultConnection"),
b => b.MigrationsAssembly("Codeflow.Registry")
)
);
services
.AddDbContext<ApplicationDbContext>(
options => options
.UseNpgsql(
Configuration.GetConnectionString("ProductionConnection"),
b => b.MigrationsAssembly("Codeflow.Registry")
)
);
在我的注册表服务中,我可以通过依赖注入获取 userManager 并创建一个帐户。默认情况下,userManager 使用 RegistryDbContext。通过注册表服务成功创建帐户后,我想在 application_db 中(通过 ApplicationDbContext)创建相同的帐户。但是我卡在这行代码中
// first I am creating the account in registry_db
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
// once the account is created, I would like to create the account on application_db
// here I am getting the dbContext for the application_db
// I can get it also over the dependency injection if needed
using (ApplicationDbContext dbCtx = new ApplicationDbContext())
{
// Here I am getting the Error. The context is ok, but an error appeard with ApplicationUser.
var test = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(dbCtx));
}
}
我收到此错误消息:
错误:没有给出与所需参数相对应的参数 格式参数 '用户管理器'的'optionsAccessor'。用户管理器(IUserStore,IOptions, IPasswordHasher, IEnumerable>, IEnumerable>,ILookupNormalizer, IdentityErrorDescriber, IServiceProbider, ILogger>)'
我的 ApplicationUser 类如下所示:
public class ApplicationUser : IdentityUser<Guid>
{
public Guid GenderId { get; set; }
[ForeignKey("GenderId")]
public Gender Gender { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime BirthDay { get; set; }
}
出了什么问题或遗漏了什么? 我已经尝试关注此帖子here,但没有运气。是否有其他方法可以创建具有其他上下文的 userManager 实例?
【问题讨论】:
-
UserManager 构造函数有很多参数,而您只指定了用户存储。您可以尝试为其他参数指定 null。
-
var test = new UserManager
(new UserStore 的 implocot 引用转换, null, null, null, null, null, null, null, null);现在我收到此消息:类型“ApplicationUser”不能用作泛型类型或方法“UserStore ”中的类型“TUser”。没有从 'ApplicationUser' 到 'Microsoft.AspNertCore.Identity.IdentityUser .
标签: c# entity-framework asp.net-core asp.net-identity