Question

I'm drawing from different questions (listed below as I ask the question).

I would like to do five things and as far as I can tell from these questions and answers... all of this can be done in my OnModelCreating method.

Preface: I am using the latest nightly bits for Asp.Net Identity 1.1-alpha

  • I would like to use "UserId" instead of Id as the property for my Users Id in the AspNetUsers table. I understand this would involve using "HasColumnName" during OnModelCreating.

  • I would like to use Guid (uniqueidentifier) as the data type for my UserId (instead of string). I understand that this involves using my own POCOS for IUser class and IUserStore, etc as Hao Kung Indicates in his answer here...

How to change type of id in Microsoft.AspNet.Identity.EntityFramework.IdentityUser

  • I would like to use my own custom table names such as MyRoles, MyCUserClaims, MyUserLogins, MyUserRoles, MyUsers as indicated in this answer...

How can I change the table names when using Visual Studio 2013 ASP.NET Identity?

  • All the foreign keys should be mapped apppropriately with the Asp.net Identity tables as Hao Kung shows here...

Create ASP.NET Identity tables using SQL script

  • I would like to add properties to the ApplicationUser of FirstName and LastName.

So, based on the above... can someone provide me with the OnModelCreating method which would create these Identity tables accordingly, with UserId as the Id column for the users of type Guid (uniqueidentifier), in tables with custom names, with the appropriate foreign key relationships, and with two additional properties on ApplicationUser of FirstName and LastName?

Was it helpful?

Solution 2

The correct answer is from this SO Post. His was not the accepted answer for that particular question but @pwdst posted an answer that includes an OnModelCreating that I modified to do exactly as I was asking here. Last week I asked him to post here so I could accept his answer but he has not done so... therefore I'm answering myself and directing you to his answer here.

OTHER TIPS

I believe you might be looking too linearly at the solution thinking it can be done all in once place.

I would advise the following:

Implement your own MembershipProvider and override the methods to use the exact tables and logic you would like for interacting with your users. If this is an ASP.NET MVC 4 or newer implementation, you may want to look into the newer SimpleMembership implementation and instead implement ExtendedMembershipProvider. SimpleMembership by default has a nice UserProfile system that will allow you to add those FirstName, LastName, and other custom properties.

Furthermore, some of the things on your list can be done independent of this implementation if you are using Entity Framework. If you are using code first you can tag the primary key of your model simply by adding the [Key] attribute to the property UserId similar to this. You can also use [Table(String name)] for the table name if it isn't the standard plural form of the POCO. Don't forget to include a reference to System.ComponentModel.DataAnnotations.

[Table("CustomUsers")]
public class User
{
    [Key]
    public Guid UserId { get; set; }
}

EDIT: Here is another q/a with many relevant links to implementing your own MembershipProvider.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top