简体   繁体   English

将 ExecuteInTransaction 与 UserManager 一起使用

[英]Using ExecuteInTransaction with UserManager

We are implementing EnableRetryOnFailure but as discussed on https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency , this does not work out of the box for user defined transactions.我们正在实施EnableRetryOnFailure但正如在https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency上所讨论的那样,这对于用户定义的事务来说是开箱即用的。

One of the recommendations from the above link (options 3 & 4) is to use an ExecutionStrategy.ExecuteInTransaction , and to call SaveChanges with acceptAllChangesOnSuccess: false .上述链接(选项 3 和 4)的建议之一是使用ExecutionStrategy.ExecuteInTransaction ,并使用acceptAllChangesOnSuccess: false调用SaveChanges

This is necessary so that the transaction can be retried if SaveChanges succeeds but the transaction fails.这是必要的,以便在 SaveChanges 成功但事务失败时可以重试事务。

The issue we are having is that we have UserManager methods wrapped up inside our transactions, and these will internally be calling SaveChanges on the context.我们遇到的问题是我们在事务中包含了UserManager方法,这些方法将在内部调用上下文中的SaveChanges

How can we do this, when we cannot change UserManager to use acceptAllChangesOnSuccess: false ?当我们无法将UserManager更改为使用acceptAllChangesOnSuccess: false时,我们该怎么做?

Adding a user with the UserManager.CreateUser method will call the SaveChanges method on the internal UserStore.使用 UserManager.CreateUser 方法添加用户将调用内部 UserStore 上的 SaveChanges 方法。 The UserStore class has a public property, AutoSaveChanges, that indicates whether to call the SaveChanges method. UserStore 类有一个公共属性 AutoSaveChanges,它指示是否调用 SaveChanges 方法。 By setting the AutoSaveChanges property to false and then calling the UserManager.CreateUser method, the SaveChanges method will not be called.通过将 AutoSaveChanges 属性设置为 false,然后调用 UserManager.CreateUser 方法,将不会调用 SaveChanges 方法。

https://github.com/dotnet/aspnetcore/blob/839cf8925278018903f53f22d580d15b0a59ca0f/src/Identity/EntityFrameworkCore/src/UserStore.cs#L141 https://github.com/dotnet/aspnetcore/blob/839cf8925278018903f53f22d580d15b0a59ca0f/src/Identity/EntityFrameworkCore/src/UserStore.cs#L141

  1. set false to UserStore.AutoSaveChanges propery.将 UserStore.AutoSaveChanges 属性设置为 false。
  2. userManager.CreateAsync(user) (no call SaveChanges) userManager.CreateAsync(user)(不调用 SaveChanges)
  3. context.SaveChanges(acceptAllChangesOnSuccess: false) context.SaveChanges(acceptAllChangesOnSuccess: false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么UserManager.GetUserAsync在使用JWT时返回null? - Why does UserManager.GetUserAsync return null when using JWT? 使用多个身份用户时,没有 Identity.UserManager 类型的服务 - No service for type Identity.UserManager when using multiple identity users 使用userManager的.NET Core身份并加载其他ApplicationUser属性 - .NET Core Identity using userManager and load additional ApplicationUser properties aspnet.core 使用 UserManager 播种数据<identityuser></identityuser> - aspnet.core Seeding data using UserManager<IdentityUser> 我可以通过使用 cookies -&gt; Asp.Net-Core Identity(实时部署)自动重新登录到我的 SignInManager 和 UserManager(身份) - Can I automatically log back into my SignInManager and UserManager (Identity) via using cookies -> Asp.Net-Core Identity (Deployed live) 在另一个 controller 中使用当前的 UserManager - Use the current UserManager in another controller 如何获取 userManager.VerifyUserTokenAsync 的目的字段? - How to get purpose field for userManager.VerifyUserTokenAsync? Identity UserManager CreateAsync 不写入数据库 - Identity UserManager CreateAsync does not write to database UserManager无法正确更新自定义身份用户 - UserManager not updating custom Identity User properly 在界面中使用 AspNetCore.Identity RoleManager 和 UserManager - Use AspNetCore.Identity RoleManager and UserManager in an Interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM