I have two databases.
- application_db
- registry_db
application_db is used for the application to get work and uses the ApplicationDbContext.
registry_db is used to manage all accounts. I would like to create first an account over my registry service and then insert the created account (with less information) into application db. Registry_db is using RegistryDbContext.
I've injected both contexts successfully (ApplicationDbContext & RegistryDbContext) inside my registry service over the dependency injection.
My Startup looks like this:
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")
)
);
Inside my registry service, I can get the userManager over the dependencies injection and create an account. On default the userManager uses the RegistryDbContext. Once an account is created successfully over the registry service, I would like to create the same account in the application_db (over the ApplicationDbContext). But I get stuck in this line of code
// 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));
}
}
I get this error message:
Error: There is no argument given that corresponds to the required format parameter 'optionsAccessor' of 'UserManager. UserManager(IUserStore, IOptions, IPasswordHasher, IEnumerable>, IEnumerable>, ILookupNormalizer, IdentityErrorDescriber, IServiceProbider, ILogger>)'
My ApplicationUser class looks like this:
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; }
}
What is wrong or missing? I tried already to follow this post here but no luck. Is there an other way to create an userManager instance with an other context?