c# - .NET Core 2.1 中的 UserManager 的 AutoSaveChanges
问题描述
我想禁用 UserManager 的自动 SaveChanges 方法调用。我发现可以通过设置 UserStore 的 AutoSaveChanges 属性来实现。但是 .NET Core 2.1 中此类事情的最佳实践是什么?是否可以通过配置 IdentityBuilder 在 Startup.cs 中执行?
解决方案
您需要在构造函数中创建一个继承 formMicrosoft.AspNetCore.Identity.EntityFrameworkCore.UserStore<IdentityUser>
并设置AutoSaveChanges
为false
的类,然后将该类注册到IServiceCollection
before AddEntityFrameworkStores
。
public class CustomUserStore : UserStore<IdentityUser>
{
public CustomUserStore(ApplicationDbContext context)
: base(context)
{
AutoSaveChanges = false;
}
}
启动.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>();
}
推荐阅读
- python - 使用 python 输出作为另一个守护进程的输入
- amazon-web-services - 对象和类型的组合 any - Terraform
- laravel - laravel - 邮递员返回 HTML 而不是 JSON?
- vue.js - VueJS Vee-Validate 验证未发生
- visual-studio-code - 如何在侧边栏底部添加项目并像 Komodo IDE 一样按需检索?
- azure-data-factory-2 - 如何在 ADF 中复制活动的列值中附加字符串值
- html - HTML 按钮位置
- python - 在元组中交换元素 - 足球日历生成器
- google-app-engine - Google Cloud PHP App Engine - signedUrl - Expired Token - 自定义错误页面
- c# - 在 C# 中展开 unix 风格的环境变量