0

As of Identity 2 they have switched from a id with an integer value (ex. 1,2,3,4,...) to a id with a nvarchar value that stores the id as some long string like

a234vt-23sdlj23klj-34jkh34jh34-23jk4jh2

If I'm creating an object that will have a single owner belonging to the person logged in, so I need to attach the user id to it, should I use this new id from Identity 2 or should I try and create some other value like an integer and put it into the aspnetusers table? Does it really matter, all I'm doing is fetching Gift object by owner(userid) and displaying/modifying gift objects on a form.

here is an example of my product object

public class Gift
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Category> Categories { get; set; }
    public int Rating { get; set; }
    public GiftStatus Status { get; set; }
    Public string UserId {get; set; //where userid is the id of the user that owns this object, should it be 3kj23jh3-h3hk1jh2-khj2h34l1b-n22g35l  ???

}
RickAndMSFT
  • 20,912
  • 8
  • 60
  • 78
chuckd
  • 13,460
  • 29
  • 152
  • 331

1 Answers1

1

There is no problem with using the Guid UserId to reference a user, if that's what you're concerned about. Unless you have a specific reason for not wanting to use a Guid as the UserId, I would suggest you just use the default behavior in order to simplify implementation. Having two separate Ids to keep track of a user sounds needlessly complicated, I wouldn't recommend that path.

If you do have a good reason for requiring an Int as the primary key instead of a Guid, you might take a look at this: http://blogs.msdn.com/b/webdev/archive/2014/03/20/test-announcing-rtm-of-asp-net-identity-2-0-0.aspx (scroll down to the "Make the type of Primary Key be extensible for Users and Roles" section). This page has a link to an example project which shows you how to use an Int as the PK. It also mentions that this extension can be used to migrate applications which use Int PKs to the new Identity 2.0.

Here's another article that may be helpful: http://www.codeproject.com/Articles/777733/ASP-NET-Identity-Change-Primary-Key

Sam
  • 4,994
  • 4
  • 30
  • 37
  • the only reason I would want to use an id is for speed. Because I will be fetching lots of objects from the db based on the guid. But if its no faster to use the guid then a regular int id, then I will jsut use the guid to keep it simple. – chuckd Jun 27 '14 at 21:15
  • If you're concerned about performance, take a look at the accepted answer to this question: http://stackoverflow.com/questions/11938044/what-are-the-best-practices-for-using-a-guid-as-a-primary-key-specifically-rega – Sam Jun 27 '14 at 21:26
  • well username is indexed, so if I need a way to store who owns what then would username be just as acceptable as id? I can't see why the default implementation of identity 2 using a nvarchar for the id would be slow in performance being that is probably the only field that would need to be fast for fetching purposes? Why would MS make the defualt implementation slow? – chuckd Jun 28 '14 at 15:55