新 ASP.NET MVC 6 标识中 AspNetUsers 表中的 ConcurrencyStamp 列的用途是什么? [英] What is the purpose of the ConcurrencyStamp column in the AspNetUsers table in the new ASP.NET MVC 6 identity?

查看:23
本文介绍了新 ASP.NET MVC 6 标识中 AspNetUsers 表中的 ConcurrencyStamp 列的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的 ASP.NET MVC 6 标识中 AspNetUsers 表中的 ConcurrencyStamp 列的用途是什么?

What is the purpose of the ConcurrencyStamp column in the AspNetUsers table in the new ASP.NET MVC 6 identity?

这是 AspNetUsers 表的数据库架构:

This is the database schema of the AspNetUsers table:

它也在 AspNetRoles 表中:

我记得它在 ASP.NET MVC 5 标识中不存在.

As I remember it wasn't there in the ASP.NET MVC 5 identity.

到目前为止,我注意到它似乎具有 GUID 值,因为它是使用以下代码定义的:

What I've noticed so far is that it seems to have GUID values as it is defined with the following code:

/// <summary>
/// A random value that must change whenever a user is persisted to the store
/// </summary>
public virtual string ConcurrencyStamp { get; set; } = Guid.NewGuid().ToString();

但是这个文档不足以让我了解它在哪些情况下使用.

But this documentation is not sufficient for me to understand in which situations it is used.

推荐答案

作为名称状态,用于防止并发更新冲突.

As the name state, it's used to prevent concurrency update conflict.

例如,数据库中有一个名为 Peter 的 UserA2个管理员打开UserA的编辑页面,想要更新这个用户.

For example, there's a UserA named Peter in the database 2 admins open the editor page of UserA, want to update this user.

  1. Admin_1 打开页面,看到名为 Peter 的用户.
  2. Admin_2 打开页面,看到名为 Peter 的用户(显然).
  3. Admin_1 将用户名更新为 Tom,并保存数据.现在 UserA 在名为 Tom 的数据库中.
  4. Admin_2 将用户名更新为 Thomas,并尝试保存.
  1. Admin_1 opened the page, and saw user called Peter.
  2. Admin_2 opened the page, and saw user called Peter (obviously).
  3. Admin_1 updated user name to Tom, and save data. Now UserA in the db named Tom.
  4. Admin_2 updated user name to Thomas, and try to save it.

如果没有 ConcurrencyStamp 会发生什么,因为 Admin_1 的更新将被 Admin_2 的更新覆盖.但是由于我们有 ConcurrencyStamp,当 Admin_1/Admin_2 加载页面时,就加载了戳记.更新数据时,此标记也会更改.所以现在第 5 步是系统抛出异常,告诉 Admin_2 该用户已经更新,因为他的 ConcurrencyStamp 与他加载的不同.

What would happen if there's no ConcurrencyStamp is Admin_1's update will be overwritten by Admin_2's update. But since we have ConcurrencyStamp, when Admin_1/Admin_2 loads the page, the stamp is loaded. When updating data this stamp will be changed too. So now step 5 would be system throw exception telling Admin_2 that this user has already been updated, since he ConcurrencyStamp is different from the one he loaded.

这篇关于新 ASP.NET MVC 6 标识中 AspNetUsers 表中的 ConcurrencyStamp 列的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆