0

I want to ask a question regarding asp.net identity and integrating custom tables. I understand that using asp.net identity system for user login and registration and other actions that relates to user membership and account management issues is easier and faster. If for example i want to build a blog and as it is known that in a blog users have to register and also a user is associated with many posts and many comments. with the default identity system, i dont know how i can include other tables i have in the database to the existing tables that has to deal with membership so that i can have one edmx diagram that maps all relationships between the identity tables and other tables in the blog. To make it more clearer ive started with my own custom implementation of a membership system which has served me well so far, but i noticed i'm missing a lot by not using the default identity system of asp.net. Lets say i have these tables below

dbo.Categories (a post belong to a category and a category contains many post

dbo.Posts

dbo.Replies

dbo.UserProfile In instances where i mention userId, AuthorId are the same and only refers to the id of the user in the Users table (if there is anything like that in asp.net identity). I

How can i have something like this and instead of my own dbo.Userprofile, i want that of the asp.net identity and then generate a model from this. I hope my question makes sense.

ibnhamza
  • 861
  • 1
  • 15
  • 29

2 Answers2

0

The central class you have to use in this scenario is IdentityUser. Based on this you can create your user class where you extend the IdentityUser class with your specific propertiers which are not part of the base class (such as Gender). It could look something like this:

public class ApplicationUser : IdentityUser<int, ApplicationUserLogin,   ApplicationUserRole, ApplicationUserClaim>
{
  public string Gender { get; set; }

  public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
  {
    // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
    var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
    // Add custom user claims here
    return userIdentity;
  }
}

Because you're using an integer as the type of the keys you have to do some customization. The answer provided by James in this thread shows you how to do it. The last thing to do is to link your existing tables to your new user table. This can be done through simply setting the foreign key.

UPDATE

Of course you should follow the hint mentioned by Chris. Missed to mention that.

Community
  • 1
  • 1
Horizon_Net
  • 5,959
  • 4
  • 31
  • 34
0

Now is as good a time as any to stop using EDMX. It's deprecated and going away completely in EF7. Identity uses Code First, so any models you want to include in the same context should also be Code First. For more information about the upcoming change and what you should do going forward see the announcement on the ADO.NET blog.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444