Tuesday, May 5, 2015

Custom asp.net identity 2.1 tables and primary keys

This is the codes that I use to set up the identity tables to use int as primary key.

public class User : IdentityUser<int, UserLogin, UserRole, UserClaim>



{
 
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User, int> 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;



}
 
public string FirstName { get; set; }

public string LastName { get; set; }

public byte Level { get; set; }

public DateTime JoinDate { get; set; }




}
 
public class UserRole : IdentityUserRole<int> { }

public class UserLogin : IdentityUserLogin<int> { }

public class UserClaim : IdentityUserClaim<int> { }

public class Role : IdentityRole<int, UserRole>



{
 
public Role() { }

public Role(string name) { Name = name; }



}
 
public class MyUserStore : UserStore<User, Role, int, UserLogin, UserRole, UserClaim>



{
 
public MyUserStore(MyPortalContext context)

: base(context) { }



}
 
public class MyRoleStore : RoleStore<Role, int, UserRole>



{
 
public MyRoleStore(MyPortalContext context)

: base(context) { }



}

/////////////////////////////////////////////////////////

And they are customized to use different table names in MyPortalContext

protected override void OnModelCreating(DbModelBuilder modelBuilder)



{
 
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<User>().ToTable("Users");

modelBuilder.Entity<Role>().ToTable("Roles");

modelBuilder.Entity<UserLogin>().ToTable("UserLogins");

modelBuilder.Entity<UserClaim>().ToTable("UserClaims");

modelBuilder.Entity<UserRole>().ToTable("UserRoles");
}

No comments: