为什么 ASP.Net Core Identity 使用字符串作为其主键属性?
Why does ASP.Net Core Identity using string for its Primary Key properties?
默认情况下,ASP.Net Core Identity 对其内置实体的主键字段使用类型 string
。例如,IdentityUser
class 将 属性 Id
作为其主键,其类型为 string
。 (这映射到 SQL 服务器中的类型 nvarchar(450)
)
如果发现这很奇怪,特别是因为默认情况下这些键的值实际上是 Guid
s:
的字符串表示
public IdentityUser()
{
Id = Guid.NewGuid().ToString();
SecurityStamp = Guid.NewGuid().ToString();
}
为什么 ASP.Net Core Identity 的设计者选择使用类型 string
作为主键而不是类型 Guid
?
目前我无法对您的问题发表评论,但我相信这可以回答您提出的问题:
Why asp.net Identity user id is string?
上面的第二个回答post:
Depending on which version of ASP.Net authentication you're using, on the database ASP.NET Identity v2 should be storing it as a uniqueidentifier (Guid) in the AspNetUsers table. In more preceding versions it will store the user id as an int in the webpages_Membership table.
I believe it surfaces it as a string so it can be any data type you like under the hood and this can then be cast within your code as required.
默认情况下,ASP.Net Core Identity 对其内置实体的主键字段使用类型 string
。例如,IdentityUser
class 将 属性 Id
作为其主键,其类型为 string
。 (这映射到 SQL 服务器中的类型 nvarchar(450)
)
如果发现这很奇怪,特别是因为默认情况下这些键的值实际上是 Guid
s:
public IdentityUser()
{
Id = Guid.NewGuid().ToString();
SecurityStamp = Guid.NewGuid().ToString();
}
为什么 ASP.Net Core Identity 的设计者选择使用类型 string
作为主键而不是类型 Guid
?
目前我无法对您的问题发表评论,但我相信这可以回答您提出的问题:
Why asp.net Identity user id is string?
上面的第二个回答post:
Depending on which version of ASP.Net authentication you're using, on the database ASP.NET Identity v2 should be storing it as a uniqueidentifier (Guid) in the AspNetUsers table. In more preceding versions it will store the user id as an int in the webpages_Membership table.
I believe it surfaces it as a string so it can be any data type you like under the hood and this can then be cast within your code as required.