Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DBContext更改后如何在控制器内创建新的UserManager对象_C#_Asp.net Core_Usermanager - Fatal编程技术网

C# DBContext更改后如何在控制器内创建新的UserManager对象

C# DBContext更改后如何在控制器内创建新的UserManager对象,c#,asp.net-core,usermanager,C#,Asp.net Core,Usermanager,我希望在控制器中的DbContext使用新连接字符串更改后有一个刷新的UserManager对象,我已将UserManager注入控制器中,但很明显,它将始终具有来自DI的最后一个DbContext引用,而不是新创建的DbContext 我试过下面的方法 this.DbContext = new ApplicationDbContext(Configuration, optionsBuilder.Options); this._userManager = new UserManager<A

我希望在控制器中的
DbContext
使用新连接字符串更改后有一个刷新的UserManager对象,我已将
UserManager
注入控制器中,但很明显,它将始终具有来自DI的最后一个
DbContext
引用,而不是新创建的
DbContext

我试过下面的方法

this.DbContext = new ApplicationDbContext(Configuration, optionsBuilder.Options);
this._userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(DbContext), null, new PasswordHasher<ApplicationUser>(), null, null, null, null, null, null);
this.DbContext=newapplicationdbcontext(配置,optionsBuilder.Options);
这是._userManager=newusermanager(newuserstore(DbContext),null,newpasswordhasher(),null,null,null,null,null,null);
它工作得很好,但缺少大部分的
UserManager
功能,如
\u userManager.CheckPasswordAsync(user,loginModel.Password)
因为我将大部分参数传递为null。
我应该怎么做才能以最简单的方式使用新的
DbContext
获得一个完全工作的
UserManger

使用依赖注入,这就是所有提到的工具的使用方式

private DbContext DbContext { get;}

private UserManager<IdentityUser> UserManager { get; } 

public MyController(DbContext dbContext, UserManager<IdentityUser> userManager)
{
    DbContext = dbContext;
    UserManager = userManager;
}
private DbContext DbContext{get;}
私有用户管理器用户管理器{get;}
公共MyController(DbContext DbContext,UserManager UserManager)
{
DbContext=DbContext;
UserManager=UserManager;
}

下面是关于如何为EF Core设置依赖项注入的页面:


假设您已经有了
服务。AddDefaultIdentity(…)
服务。AddDbContext(…)
,注入
UserManager
YourProjectDbContext
应该只在您的控制器中工作。

您可以尝试创建一个服务并在服务中使用UserManager,然后,使用Startup.ConfigureServices方法中的瞬态操作配置服务。临时操作总是不同的,每次检索服务时都会创建一个新实例。然后,您可以在控制器中使用此服务。请检查以下步骤:

创建UserManagerRepository服务(在此服务中,您可以创建方法并使用UserManager方法):

截图如下:

参考:


只是好奇为什么要做这样的事情?因此,再次调用服务后,userManager将使用新创建的AppDbContext进行初始化?userManager仍然保留对旧dbContext的引用,新dbContext具有不同的connectionString@SabirHossain当然因为代码注入了身份模型提供的
UserManager
,这仍然取决于配置的
DbContext
(在应用程序启动时)。这个答案肯定不是你想要的。我知道,@appect我该怎么做才能用新创建的DbContext刷新UserManger,你可以看到我是如何做到的,但我需要一个功能齐全的UserManager
public interface IUserManagerRepository
{
    void Write(string message);
}
public class UserManagerRepository : IUserManagerRepository, IDisposable
{
    private bool _disposed;
    private readonly UserManager<IdentityUser> _userManager;

    public UserManagerRepository(UserManager<IdentityUser> userManager)
    {
        _userManager = userManager;
    }

    public void Write(string message)
    {
        // _userManager.ChangePasswordAsync()
        Console.WriteLine($"UserManagerRepository: {message}");
    }

    public void Dispose()
    {
        if (_disposed)
            return;

        Console.WriteLine("UserManagerRepository.Dispose");
        _disposed = true;
    }
}
 services.AddTransient<IUserManagerRepository, UserManagerRepository>();
    public IActionResult Index()
    {
        var services = this.HttpContext.RequestServices;
        var log = (IUserManagerRepository)services.GetService(typeof(IUserManagerRepository));

        log.Write("Index method executing");
         
        var log2 = (IUserManagerRepository)services.GetService(typeof(IUserManagerRepository));

        log2.Write("Index method executing");
        var log3 = (IUserManagerRepository)services.GetService(typeof(IUserManagerRepository));

        log3.Write("Index method executing"); 
        return View();
    }