.NET Core 2.1中UserManager的AutoSaveChanges [英] UserManager's AutoSaveChanges in .NET Core 2.1
问题描述
我想禁用UserManager的自动SaveChanges方法调用.我发现可以通过设置UserStore的AutoSaveChanges属性来做到这一点.但是,.NET Core 2.1中此类事情的最佳实践是什么?是否可以通过配置IdentityBuilder在Startup.cs中进行操作?
I would like to disable automatic SaveChanges method calls of UserManager. I've found that it is possible to do it by setting AutoSaveChanges property of UserStore. But what is the best practice for such things in .NET Core 2.1? Is it possible to do in Startup.cs by configuring IdentityBuilder?
推荐答案
您需要创建一个继承 Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore< IdentityUser>
形式的类并设置在构造函数中将AutoSaveChanges
更改为 false
,然后在 AddEntityFrameworkStores
之前将此类注册到 IServiceCollection
.
You need to create a class that inherits form Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore<IdentityUser>
and set AutoSaveChanges
to false
in the constructor and then register this class to IServiceCollection
before AddEntityFrameworkStores
.
public class CustomUserStore : UserStore<IdentityUser>
{
public CustomUserStore(ApplicationDbContext context)
: base(context)
{
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>();
}
这篇关于.NET Core 2.1中UserManager的AutoSaveChanges的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!