Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.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# 用户管理器&x27;s自动保存.NET Core 2.1中的更改_C#_.net_Asp.net Web Api_Asp.net Core - Fatal编程技术网

C# 用户管理器&x27;s自动保存.NET Core 2.1中的更改

C# 用户管理器&x27;s自动保存.NET Core 2.1中的更改,c#,.net,asp.net-web-api,asp.net-core,C#,.net,Asp.net Web Api,Asp.net Core,我想禁用UserManager的自动SaveChanges方法调用。我发现可以通过设置UserStore的AutoSaveChanges属性来实现。但是,在.NETCore2.1中,这种事情的最佳实践是什么?是否可以通过配置IdentityBuilder在Startup.cs中执行此操作?您需要创建一个继承Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore格式的类,并在构造函数中将AutoSaveChanges设置为false,

我想禁用UserManager的自动SaveChanges方法调用。我发现可以通过设置UserStore的AutoSaveChanges属性来实现。但是,在.NETCore2.1中,这种事情的最佳实践是什么?是否可以通过配置IdentityBuilder在Startup.cs中执行此操作?

您需要创建一个继承Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore格式的类,并在构造函数中将
AutoSaveChanges
设置为
false
,然后将该类注册到
IServiceCollection
AddEntityFrameworkStores

public class CustomUserStore : UserStore<IdentityUser>
{
    public CustomUserStore(ApplicationDbContext context)
        : base(context)
    {
        AutoSaveChanges = false;
    }
}
公共类CustomUserStore:UserStore
{
公共CustomUserStore(ApplicationDbContext上下文)
:基本(上下文)
{
AutoSaveChanges=false;
}
}
Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<IUserStore<IdentityUser>, CustomUserStore>();

    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("DefaultConnection")));

    services.AddDefaultIdentity<IdentityUser>()
        .AddDefaultUI(UIFramework.Bootstrap4)
        .AddEntityFrameworkStores<ApplicationDbContext>();
}
public void配置服务(IServiceCollection服务)
{
services.addScope();
services.AddDbContext(选项=>
options.UseSqlServer(
GetConnectionString(“DefaultConnection”);
services.AddDefaultIdentity()
.AddDefaultUI(UIFramework.Bootstrap4)
.AddEntityFrameworkStores();
}

然后默认的用户管理器将拾取并使用它?是的,
UserManager
依赖于
IUserStore
,它由
services.AddScoped()
注册。很遗憾,我现在无法测试,但听起来很有希望!我要了