5

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?

Harry
  • 489
  • 1
  • 5
  • 13
  • The UserManager constructor has many parameters, and you have specified only user store. You can try specifying null for other params. – Thangadurai Apr 03 '18 at 09:17
  • var test = new UserManager(new UserStore, null, null, null, null, null, null, null, null); Now i get this message: The type "ApplicationUser" cannot be used as a type 'TUser' in the generic type or method 'UserStore'. There is no implocot reference conversion from 'ApplicationUser' to 'Microsoft.AspNertCore.Identity.IdentityUser. – Harry Apr 03 '18 at 11:50

3 Answers3

1
using (var db = new TenantDBContext("connectionString"))
{
    db.Database.Migrate();
    var store = new UserStore<IdentityUser>(db);
    var user = new IdentityUser("test");
    var result = await store.CreateAsync(user);
}
Batu Tem
  • 11
  • 1
0

It is dependency injection over constructor. With DI, you shouldn't initialize your service directly. You should register your Service and ApplicationContext in Startup.cs.

When you need your Service, you should inject it over constructor into a controller, and DI chain will automatically inject instance of ApplicationContext into the Service.

Of course, if you don't need your service for each method in a controller, you can initialize it into method :

[Route("api/[controller]")]
public class MyController : Controller
{
    [HttpGet]
    public async IEnumerable<object> Get([FromServices] ApplicationContext context,
                                         MyType myMainParam)
    {
        ...
    }
}

However what you really need to do is to implement your own version of UserManagerFactory which this link might be of help.

this post might help too.

P.S :

Take a look at @Singularity222 answer in this thread. he has done the same thing you wanna do, in his repo.

Also we have this thread where the owner of Identity project (!) said you need to implement your own version of UserStore.

AmiNadimi
  • 5,129
  • 3
  • 39
  • 55
  • I think you didn't understand the question in my post. I have not a problem with injecting the dbContext. My only problem is that I can't create an userManager with an different context. var test = new UserManager(new UserStore, null, null, null, null, null, null, null, null); Gives this error message: The type "ApplicationUser" cannot be used as a type 'TUser' in the generic type or method 'UserStore'. There is no implocot reference conversion from 'ApplicationUser' to 'Microsoft.AspNertCore.Identity.IdentityUser. – Harry Apr 03 '18 at 12:40
  • @Harry take a look at new update, I hope it will help. – AmiNadimi Apr 04 '18 at 06:55
0

I think you are referencing the wrong assembly.

The UserManager class which its constructor receive only one argument UserStore is not ASP.Net Core UserManager, it is ASP.Net UserManager. Please check the following details,

ASP.Net UserManager (Microsoft.AspNet.Identity.Core.dll): https://msdn.microsoft.com/en-us/library/dn468199(v=vs.108).aspx

ASP.Net Core UserManager (Microsoft.AspNetCore.Identity.dll, Microsoft.Extensions.Identity.Core.dll): https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.identity.usermanager-1?view=aspnetcore-2.0

So my suggestion is, fix your identity library reference to use .Net Core one.

Laksmono
  • 690
  • 5
  • 12
  • If I'm understanding correctly, you're suggestion is to use a .Net library in a .Net Core project? I'm not sure if this would work or not, but I definitely would not recommend it. – devklick Dec 11 '20 at 19:17
  • you're misunderstood, Read the question properly, based on the error message stated, it is clear that he is using .net library for his .net core project. So my suggestion was to fix the reference to use the .net core one. – Laksmono Dec 14 '20 at 04:21