Generating an identifier for a managed user identifier

I changed the default MVC5 template, instead of using string/ nvarchar-keyed users to use Guids / uniqueidentifiers. My solution was similar to that discussed here: http://blogs.msdn.com/b/webdev/archive/2013/12/20/announcing-preview-of-microsoft-aspnet-identity-2-0-0-alpha1. aspx

I changed the type parameters where applicable, but my first user was generated with the identifier 00000000-0000-0000-0000-000000000000. The second user could not be created because its primary key contradicted the first.

Then I changed the applicable type parameters from Guidto int, and then it worked with user identifiers starting with 1 and increasing.

So how do I get it to work with Guid?

I will need to hook somewhere and assign a new Guid to each newly created user. Where is the best place for this? I was thinking maybe in the ApplicationUser constructor (implements IdentityUser), but I was not sure.

+4
source share
1 answer

I found that the Tieson T comment is the correct answer, but it was sent as a comment and not an answer, so I will reproduce my specific solution here. In my class ApplicationUser(implements IdentityUser) I have overridden the property Idand added the System.ComponentModel.DataAnnotations.KeyAttributeand attributes System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOptionAttribute. My ApplicationUser class is as follows:

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

Source: https://habr.com/ru/post/1544629/


All Articles