Zan*_*ski 4 c# asp.net asp.net-mvc asp.net-identity-2
我已经将默认的MVC5模板修改为使用s / ,而不是使用string/ nvarchar键用户。我的解决方案类似于此处讨论的解决方案:http : //blogs.msdn.com/b/webdev/archive/2013/12/20/announcing-preview-of-microsoft-aspnet-identity-2-0-0- alpha1.aspxGuiduniqueidentifier
我在适用的地方更改了类型参数,但生成的第一个用户的ID为00000000-0000-0000-0000-000000000000。由于第二个用户的主键与第一个用户的主键冲突,因此无法创建。
然后,我将适用的类型参数从更改Guid为int,然后使用从1开始递增的用户ID起作用。
那么我该如何使用它Guid呢?
我必须需要挂接到某个地方,并为每个新创建的用户分配一个新的Guid。最好的地方在哪里?我以为也许在ApplicationUser(实现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; }
}
...
}
Run Code Online (Sandbox Code Playgroud)