简体   繁体   English

删除具有父级的ApplicationUser时的OptimisticConcurrencyException

[英]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.

相关问题 删除父项时删除子项 - Deleting Child Entries when deleting parent &#39;没有注册&#39;Identity.UserManager`1[ProjectName.ApplicationUser]&#39;类型的服务。容器销毁时可用)&#39; - 'No service for type 'Identity.UserManager`1[ProjectName.ApplicationUser]' has been registered.able when container is destroy)' 返回PartialView时ApplicationUser为null - ApplicationUser is null when returning PartialView 使用存根实体更新实体关系时捕获了optimisticconcurrencyexception - optimisticconcurrencyexception was caught when updating entity relation using stub entities 如何正确设置 ApplicationUser 和 FriendRequest 实体以允许级联删除 - How to properly setup ApplicationUser and FriendRequest entities to allow for cascade deleting 与父母/孩子一起删除问题 - Problem Deleting with Parent/Child MVC应用程序中的OptimisticConcurrencyException - OptimisticConcurrencyException in MVC application 添加声明时出现错误:“无效的列名 &#39;ApplicationUser_Id&#39;” - Error: “Invalid column name 'ApplicationUser_Id'” when adding claims 当包括类型ApplicationUser的属性时,“为此实体类型定义键。” - “Define the key for this EntityType.” when including attribute of type ApplicationUser 具有ApplicationUser属性的实体,当我在服务中使用它时,该属性为null - Entity with ApplicationUser property, when i use it in a service the property is null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM