[英]OptimisticConcurrencyException when deleting an ApplicationUser that has a parent
I've extended ApplicationUser by adding a parent entity: 我通过添加父实体扩展了ApplicationUser:
public class ApplicationUser : IdentityUser
{
public int MasterId { get; set; }
public virtual Master Master { get; set; }
}
public class Master
{
public int Id { get; set; }
public string Name { get; set; }
}
And the related DbContext reference to the new entity 以及相关的DbContext引用新实体
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Master> Masters { get; set; }
}
Now, when I try to delete an ApplicationUser, userManager.UpdateAsync()
throws an OptimisticConcurrencyException
. 现在,当我尝试删除ApplicationUser时,
userManager.UpdateAsync()
会抛出OptimisticConcurrencyException
。
public class HomeController : Controller
{
public async Task<ActionResult> Index()
{
// Get the DbContext and UserManager out of the OWIN pipeline
var db = GetDbContext(this);
var userManager = GetUserManager(this);
// Create a Master record if necessary
if (db.Masters.Count() == 0) {
db.Masters.Add(new Master { Name = "The Master" });
await db.SaveChangesAsync();
}
var masterId = (from m in db.Masters select m.Id).First();
// Create a new ApplicationUser
var user = new ApplicationUser {
UserName = "Bob",
Email = "abc@abc.com",
MasterId = masterId
};
await userManager.CreateAsync(user, "Password123?");
await userManager.UpdateAsync(user);
// Try to delete the ApplicationUser
user = (from u in db.Users select u).First();
await userManager.DeleteAsync(user);
await userManager.UpdateAsync(user); // Throws OptimisticConcurrencyException
return View();
}
public static ApplicationDbContext GetDbContext(Controller controller)
{
return controller.HttpContext.GetOwinContext()
.Get<ApplicationDbContext>();
}
public static ApplicationUserManager GetUserManager(Controller controller)
{
return controller.HttpContext.GetOwinContext()
.GetUserManager<ApplicationUserManager>();
}
Just to reiterate, my data model has no concurrency-checking members. 重申一下,我的数据模型没有并发检查成员。 The above code is cut-and-pasted from a newly created MVC app which I modified by adding a parent entity to the ApplicationUser entity.
上面的代码是从新创建的MVC应用程序中剪切并粘贴的,我通过向ApplicationUser实体添加父实体来修改该应用程序。
Another note: In researching this issue, I ran across an SO post (which I can not find now) from several years ago that claimed that this is a known bug, to wit Identity gets confused when it tries to update the related records and sees that nothing (in the parent record) has changed. 另一个注意事项:在研究这个问题时,几年前我遇到了SO帖子(我现在找不到),声称这是一个已知的bug,因为当它试图更新相关记录并看到没有(在父记录中)已经改变。
And another note: I tried making the MasterId
foreign key nullable, and set it to null before attempting to delete the user. 另一个注意事项:我尝试使
MasterId
外键可以为空,并在尝试删除用户之前将其设置为null。 Didn't help. 没有帮助。 The problem seems to be the presence of the foreign key in the
ApplicationUser
entity, whether or not the FK has a value. 问题似乎是
ApplicationUser
实体中存在外键,无论FK是否具有值。
The call to UpdateAsync(user)
is not needed, and is probably causing your problem after the DeleteAsync(user)
since the user has been deleted. 不需要调用
UpdateAsync(user)
,并且可能会在DeleteAsync(user)
之后导致您的问题,因为用户已被删除。 Remove the update calls and you should be good 删除更新调用,你应该是好的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.