簡體   English   中英

如何在 ASP.NET Core 中更改 userManager 的 dbContext?

[英]How can I change the dbContext of userManager in ASP.NET Core?

我有兩個數據庫。

  • 應用數據庫
  • 注冊表_數據庫

application_db用於應用程序獲取工作並使用ApplicationDbContext

registry_db用於管理所有帳戶。 我想首先通過我的注冊表服務創建一個帳戶,然后將創建的帳戶(信息較少)插入應用程序 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));
    }
}

我收到此錯誤消息:

錯誤:沒有給出與“UserManager”的所需格式參數“optionsAccessor”相對應的參數。 UserManager(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; }
}

有什么問題或遺漏了什么? 我已經嘗試在這里關注這篇文章但沒有運氣。 有沒有其他方法可以使用其他上下文創建 userManager 實例?

它是對構造函數的依賴注入。 使用 DI,您不應該直接初始化您的服務。 您應該在Startup.cs注冊您的 Service 和 ApplicationContext。

當你需要你的服務時,你應該通過構造函數將它注入控制器,DI 鏈會自動將 ApplicationContext 的實例注入服務。

當然,如果您不需要為控制器中的每個方法提供服務,您可以將其初始化為 method :

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

但是,您真正需要做的是實現您自己的UserManagerFactory版本, 此鏈接可能會有所幫助。

這篇文章也可能有所幫助。

附注:

看看這個線程中的@Singularity222答案。 他在他的 repo 中做了你想做的同樣的事情。

我們還有這個帖子,其中Identity 項目所有者(!) 說您需要實現自己的UserStore版本。

我認為您引用了錯誤的程序集。

其構造函數僅接收一個參數 UserStore 的 UserManager 類不是 ASP.Net Core UserManager,而是 ASP.Net UserManager。 請檢查以下詳細信息,

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

ASP.Net 核心用戶管理器(Microsoft.AspNetCore.Identity.dll、Microsoft.Extensions.Identity.Core.dll): https : //docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.identity.usermanager -1?view=aspnetcore-2.0

所以我的建議是,修復您的身份庫引用以使用 .Net Core 之一。

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM