Search code examples
asp.net-coreasp.net-core-identity

Why does ASP.Net Core Identity using string for its Primary Key properties?


By default, ASP.Net Core Identity uses the type string for the primary key fields of its built-in entities. For example, the IdentityUser class has the property Id as its primary key and its type is string. (This is mapped to type nvarchar(450) in SQL Server)

If find this peculiar, especially since by default the values of these keys are actually string representations of Guids:

    public IdentityUser()
    {
        Id = Guid.NewGuid().ToString();
        SecurityStamp = Guid.NewGuid().ToString();
    }

Why have the designers of ASP.Net Core Identity chosen to use type string for primary keys over, says, type Guid?


Solution

  • I cannot comment on your question at this time, but I believe this has the answers to the question you are asking:

    Why asp.net Identity user id is string?

    Second answer from above post: https://stackoverflow.com/a/30029268/11191898

    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.