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