0

I use ASP.NET Identity 2.0 in my MVC5 project and wanted to change the data type of id field of AspNetUsers from string (GUID) to int because of that I have some tables related to AspNetUsers table. I look at many pages as on How to change type of id in Microsoft.AspNet.Identity.EntityFramework.IdentityUser but it seems to be not easy to apply. On the other hand, I see that changing the data type of id field of AspNetUsers entity will not be enough as there are some pk/fk relations between the other identity types i.e. AspNetUserRoles, ApplicationGroupRoles, etc.. In that case, should I use the original data type for identity tables and change the id type for my tables from int to string? Or how can I change data type of all of the identity tables easily in ASP.NET Identity 2.0? If this is easier in version 3.0 I can also use it.

Community
  • 1
  • 1
Jack
  • 1
  • 21
  • 118
  • 236
  • Why you don't let Users with string Id and all others tables with int Id? – Lucian Bumb Jul 31 '16 at 06:02
  • @LucianBumb I am not sure if it is good idea for PK/FK relation. So, I think the best idea is to use int for custom tables and convert id type from string to int for asp.net identity tables. Any suggestion? – Jack Jul 31 '16 at 19:51
  • mate, I struggle a lot to do what you are trying now, because I was sure that I cannot have `string` Id in some entities, and `int` in other entities, till I decide to make a test and see what will be the problem. The result was no problem! you just need to take care, that `userId` is `string`. – Lucian Bumb Jul 31 '16 at 20:05

1 Answers1

0

I want to extend my comment because, few months ago I was trying to do the same.

In my case, I was not sure if I can create relations string -> int, comming from Database first, and used to create relations between productId(int) and userId(int), I wanted to have int ids all over.

Mate you can let userId like string and create relations like this;

public class Customer
{
  public int Id {get;set;}
  public string Name {get;set;}

  public string SalesManId {get;set;}
  [ForeignKey("SalesManId ")]
  public virtual ApplicationUser SalesMan {get;set;}
}

Here is the Customer Entity, and I have declared a navigation property! So, the SalesManId is of type string. In this way you can create all your relations with ApplicationUser class.

If you still want to change to int, here is a youtube tutorial, which I followed few months ago and I manged to change from string to int

YouTube tutorial-How to change UserId from string to int

Lucian Bumb
  • 2,821
  • 5
  • 26
  • 39
  • Many thanks for your reply. Actually I had encountered many problems during pk/fk relationships creations and I think it would be better if I solve the problem by changing data type of identity tables from string to int. >>> – Jack Jul 31 '16 at 20:38
  • So, I look at the original post on http://www.asp.net/identity/overview/extensibility/change-primary-key-for-users-in-aspnet-identity but not sure if I have to add the custom classes to IdentityModels explained at step "Add customized Identity classes that use the key type". Because I am creating the project now and there is no table created currently. I mean that can I modify the current classses instead of adding the custom ones? – Jack Jul 31 '16 at 20:38
  • @binary I insert in the answer a link to the tutorial which is telling step by step how to do it. – Lucian Bumb Jul 31 '16 at 20:49