gpt4 book ai didi

c# - 为 Guid-keyed IdentityUser 生成 id

转载 作者:太空狗 更新时间:2023-10-30 01:04:15 25 4
gpt4 key购买 nike

我修改了默认的 MVC5 模板,而不是使用 string/nvarchar-keyed 用户,而是使用 Guids/唯一标识符。我的解决方案类似于此处讨论的解决方案:http://blogs.msdn.com/b/webdev/archive/2013/12/20/announcing-preview-of-microsoft-aspnet-identity-2-0-0-alpha1.aspx

我在适用的地方更改了类型参数,但我的第一个用户生成的 ID 为 00000000-0000-0000-0000-000000000000。无法创建第二个用户,因为它的主键与第一个用户的主键冲突。

然后我将适用的类型参数从 Guid 更改为 int,然后它起作用了,用户 ID 从 1 开始并递增。

那么如何让它与 Guid 一起工作呢?

我只需要在某个地方 Hook 并​​为每个新创建的用户分配一个新的 Guid。最好的地方在哪里?我想也许在 ApplicationUser (implements IdentityUser ) 构造函数中,但我不确定。

最佳答案

我发现 Tieson T 的评论代表了正确答案,但它是作为评论而不是答案发布的,因此我将在此处复制我的具体解决方案。在我的 ApplicationUser 类(实现 IdentityUser)中,我覆盖了 Id 属性并添加了 System.ComponentModel.DataAnnotations.KeyAttributeSystem.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOptionAttribute 属性。因此,我的 applicationUser 类如下所示:

public class ApplicationUser : IdentityUser<Guid, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public override Guid Id
{
get { return base.Id; }
set { base.Id = value; }
}
...
}

关于c# - 为 Guid-keyed IdentityUser 生成 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24227696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com