C# 用户管理器&x27;s自动保存.NET Core 2.1中的更改
我想禁用UserManager的自动SaveChanges方法调用。我发现可以通过设置UserStore的AutoSaveChanges属性来实现。但是,在.NETCore2.1中,这种事情的最佳实践是什么?是否可以通过配置IdentityBuilder在Startup.cs中执行此操作?您需要创建一个继承Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore格式的类,并在构造函数中将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,
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()
注册。很遗憾,我现在无法测试,但听起来很有希望!我要了