[英]DetectChanges when using AspNet.Identity UserManager
I am using AspNet.Identity
for User Management in my MVC project because I believe it is a great start, now that I have it working (with little changes), I wanted to add an Audit Trail (Track Changes) like my other DbContext that I use. 我在我的MVC项目中使用AspNet.Identity
进行用户管理因为我认为这是一个很好的开始,现在我已经开始工作了(几乎没有变化),我想像我的其他DbContext那样添加一个审计跟踪(Track Changes)我用。
In IdentityModel.cs
, I have the following code that works, but only in certain situations: 在IdentityModel.cs
,我有以下代码可以工作,但只在某些情况下:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
//Can't recall if this was there by default
Configuration.AutoDetectChangesEnabled = true;
}
public override int SaveChanges()
{
//Tell EF to Track Changes
ChangeTracker.DetectChanges();
//More code once I get this working
//
}
}
In my Controller, I have the following: 在我的控制器中,我有以下内容:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit(EditUserViewModel editUser)
{
var user = await UserManager.FindByIdAsync(editUser.Id);
//Update a property within the User object
user.FirstName = "Updated First Name";
//Save to database
var result = UserManager.Update(user);
//The above saves to database, but doesn't trigger SaveChanges()
//SaveChanges() will be triggered if I call
HttpContext.GetOwinContext().Get<ApplicationDbContext>().SaveChanges();
}
When the above HttpContext.GetOwinContext().Get<ApplicationDbContext>().SaveChanges();
当上面的HttpContext.GetOwinContext().Get<ApplicationDbContext>().SaveChanges();
is called, the updated ApplicationUser has an EntityState
of Unchanged
. 调用时,更新的ApplicationUser的EntityState
为Unchanged
。 Makes sense as it was already saved. 因为它已经被保存了,所以有道理。
However, I am trying to see how I can utilize the UserManager
and still work with SaveChanges()
. 但是,我试图了解如何使用UserManager
并仍然使用SaveChanges()
。
I also understand that I could write a class that would log all of this myself, but as I expand the ApplicationUser (or ApplicationRole) I would like to avoid the extra coding for the Audit Log. 我也明白我可以写一个自己记录所有这些的类,但是当我扩展ApplicationUser(或ApplicationRole)时,我想避免对审计日志进行额外的编码。
Any advice or links would help! 任何建议或链接都会有所帮助!
Set AutoSaveChanges to false in the constructor of your UserStore (which you would pass to the constructor of the UserManager). 在UserStore的构造函数中将AutoSaveChanges设置为false(您将传递给UserManager的构造函数)。
public class MyUserStore : UserStore<User, Role, int, UserLogin, UserRole, UserClaim>
{
private MyDbContext _context;
public MyUserStore(MyDbContext context) : base(context)
{
//Set this to false
AutoSaveChanges = false;
_context = context;
}
}
Now you can call save changes as you normally would and the change tracker will contain the changes you're expecting. 现在,您可以像往常一样调用保存更改,更改跟踪器将包含您期望的更改。
I did however encounter an OptimisticConcurrencyException when I tried to make more than one call to UserManager between calls to SaveChanges. 但是当我尝试在调用SaveChanges之间对UserManager进行多次调用时,我遇到了一个OptimisticConcurrencyException。 I just called SaveChanges after each UserManager operation. 我在每次UserManager操作后调用了SaveChanges。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.