我修改了默认的 MVC5 模板,而不是使用 string
/nvarchar
-keyed 用户,而是使用 Guid
s/唯一标识符
。我的解决方案类似于此处讨论的解决方案: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.KeyAttribute
和 System.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/