3

Current project:

  • ASP.NET 4.5.2
  • MVC 5
  • Identity 2.2.1

One of the problems I am running into is that I am making use of GUIDs properly throughout the rest of the project, with the DB field types being UniqueIdentifier. Unfortunately, Identity does not play by the same rules, and for some inscrutable reason they created the table fields as a string -- nVarChar(128).

I have found a few hits out there that show how to properly modify Identity to use UniqueIdentifier and cast the Guids to the proper format, however all these resources are currently obsolete - most are for pre-v2 and the most recent one was for 2.0.0-alpha, and the code it provided doesn’t match up or work on the current version of Identity.

I have done my best to Google what is out there, but either it doesn’t exist or my Google-fu is weak today. I am looking for any pointers on how to change the DB field to a UniqueIdentifier (as proper for a Guid) while still have everything else work/cast properly.

EDIT: It seems that a person or system has linked this article as a possible duplicate to another article. Problem is, I already linked to that other article as being a v1.x usage of Identity, and as such, not directly applicable. Even the 2.0.0-alpha code that I linked to does not work. Even the additional material at the bottom of the linked article is not functional under 2.2.1.

Community
  • 1
  • 1
René Kåbis
  • 842
  • 2
  • 9
  • 28
  • 1
    Sorry, not a duplicate. I am not trying to change a GUID to an Integer, I am trying to change the DB field from nvarchar(128) to UniqueIdentifier. I am still using GUIDs, but instead of a string field, I want the proper UniqueIdentifier field. I have yet to build my DB, so this is the perfect time to make sure that Identity creates UniqueIdentifier primary key fields instead of nvarchar(128) fields. – René Kåbis Apr 29 '16 at 21:21
  • And I am saying the instructions are causing Identity 2.2.1 to leave the project painted red in nearly every section. At least the controllers are expected -- they are working with strings and are suddenly confronted by Guids. But when my `ApplicationDbContext()` gets painted via the `throwIfV1Schema`, as well as my `GenerateUserIdentityAsync` via the `manager` there are deeper issues going sideways that are not being covered by that example. – René Kåbis Apr 29 '16 at 21:29
  • Understood. I will remove suggestion. – Nkosi Apr 29 '16 at 21:31
  • Re "controllers working with strings and are suddenly confronted by Guids". You'll have to fix your controllers to work with Guids. And sample you link for 2.0.0-alpha is mostly correct. Current released Identity is not much different. – trailmax Apr 30 '16 at 23:07
  • And have you seen this guide? http://www.asp.net/identity/overview/extensibility/change-primary-key-for-users-in-aspnet-identity It converts id into `int` but principle is the same. – trailmax Apr 30 '16 at 23:09
  • Yes, and unfortunately I still get paint in four places, two in the IndentityConfig and two in the Startup.Auth. Both in the former deal with `UserStore` which is now `CustomUserStore`. The latter gets painted at `regenerateIdentity:` whereas in the example it was `regenerateIdentityCallback:`. Changing that to the example paints everything after `SecuritySTampValidator`. – René Kåbis May 02 '16 at 23:08
  • You can use GUID bu you've make it if now use code first migrations. – Archil Labadze Jun 05 '16 at 15:32
  • Actually, I have extensively tested this, and you cannot use Guid within EF6 because it does not implement IConvertible, whereas Guid does. To be specific, the entire edifice falls flat on its face because of that one problem. Identity Management *needs* to be able to convert the ID into other formats, which is what Guid allows. – René Kåbis Jun 05 '16 at 16:05

1 Answers1

-3

Maybe this will help:

Entity Framework – Use a Guid as the primary key

UPDATE So if you will use GUID like this:

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }

EF will generate uniqueidentifier in your DB table, but it's important that column value to be set to (newsequentialid()) and make StoreGeneratedPattern to Identity. So this will automaticaly generate next unique ID

using (ApplicationDbContext context = new ApplicationDbContext())
{
    var item = new Item
                 {
                     Name = "Random",
                     Description= "Person";
                 };

    context.Items.Add(item);
    context.SaveChanges();
    Console.WriteLine(item.Id);
}

Home this will helpfull.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Archil Labadze
  • 4,049
  • 4
  • 25
  • 42
  • This works for any other table you might want to manually create. This doesn’t work for Identity 2.x, which prevents you from directly modifying the tables it makes use of. There is no-where in the project where you can directly change the primary key of any Identity table as you do above. – René Kåbis Jun 15 '16 at 18:36