1

I have below four tables

Role table

enter image description here

User table

enter image description here

UserRole table

enter image description here

UserType table

enter image description here

default Asp.net identity having below tables like Asp.netusers,Asp.netRoles,Asp.netuserlogins,Asp.netuserclaims,Asp.netuserroles

My table doesn't match the same column name and some columns not available in my tables. can i use my own tables to utilize the feature of asp.net identity or else i need to follow the same columns used in Asp.netusers table to my User table.

Is that all columns necessary to add in my table ?

Already I have implemented asp.net identity EF database first approach with same default tables. I have separate role store and user store

context below here users,userroles are same default table as like in asp.net identity(asp.netusers,asp.netroles)

public partial class OVT_UserEntities : DbContext
    {
        public OVT_UserEntities()
            : base("name=OVT_UserEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<UserClaim> UserClaims { get; set; }
        public virtual DbSet<UserLogin> UserLogins { get; set; }
        public virtual DbSet<UserRole> UserRoles { get; set; }
        public virtual DbSet<User> Users { get; set; }
    }

User class:

public partial class User : IUser<int>
    {
    }

Role class:

public partial class UserRole : IRole<int>
    {
    }

Now i want to use above four new tables(table design images above) instead existing table provided by asp.net identity. So i am not sure whether same all the tables and columns need to be added in my database to work on asp.net identity ?

7
  • You can definitely use your own table structure Commented Apr 29, 2016 at 4:50
  • @monstertjie_za could you please redirect some link or article to follow up the same ? Commented Apr 29, 2016 at 4:53
  • How are you wanting to use the tables you have described? Do your tables have any relation to the first set? Column names don't matter in much of any way aside from making things visually identifiable to others. The main thing would be to ensure identical data types and then use foreign key constraints to ensure data integrity. Commented Apr 29, 2016 at 4:54
  • @gmiley updated by question. role ,user,userrole table same like asp.netroles,asp.netusers,asp.netuserroles table. but some columns not available in one of the tables. my question is whether all the columns as like asp.net identity we have to design or not ? Commented Apr 29, 2016 at 5:18
  • @gmiley if you have any working sample or article with own table definiton that would be better to understtand ? Commented Apr 29, 2016 at 5:19

1 Answer 1

4

Since you're using Microsoft.AspNet.Identity you should inherit your User from IdentityUser (namespace Microsoft.AspNet.Identity.EntityFramework).

Your classes should be defined like this:

USER

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

ROLE

public class Role : IdentityRole<int, UserRole>
{
}

USER-ROLE

public class UserRole : IdentityUserRole<int>
{
}

USER-CLAIM

public class UserClaim : IdentityUserClaim<int>
{
}

USER-LOGIN

public class UserLogin : IdentityUserLogin<int>
{
}

You could extend the classes adding your own custom columns:

public class User : IdentityUser<int, UserLogin, UserRole, UserClaim>
{
    public string CompanyName { get; set; }
}

Now you have to define the stores:

public class UserStore:  UserStore<User, Role, int, UserLogin, UserRole, UserClaim>
{
    public UserStore(MyContext context)
        : base(context)
    {
    }
}

and then your database context, inheriting from IdentityDbContext:

public class MyContext : IdentityDbContext<User, Role, int, UserLogin, UserRole, UserClaim>
{
    public MyContext(): base("ConnectionString")
    {

    }
}

In your database context (MyContext) you must override OnModelCreating so that you can make your columns, change types, tables names etc etc:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<MyUser>()
            .Property(p => p.Id)
            .HasColumnType("int")
            .IsRequired();

        modelBuilder.Entity<MyRole>()
            .Property(p => p.Id)
            .HasColumnType("int")
            .IsRequired();

        modelBuilder.Entity<MyUserRole>()
            .Property(p => p.RoleId)
            .HasColumnType("int")
            .IsRequired();

        modelBuilder.Entity<MyUserRole>()
            .Property(p => p.UserId)
            .HasColumnType("int")
            .IsRequired();

        modelBuilder.Entity<MyUserClaim>()
            .Property(p => p.Id)
            .HasColumnType("int")
            .IsRequired();

        modelBuilder.Entity<MyUserClaim>()
            .Property(p => p.UserId)
            .HasColumnType("int")
            .IsRequired();

        modelBuilder.Entity<MyUserLogin>()
            .Property(p => p.UserId)
            .HasColumnType("int")
            .IsRequired();

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

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

        modelBuilder.Entity<MyUserRole>()
            .ToTable("UserRoles");

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

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

    }

Now you can use migrations to generate your tables.

I've update a github project to reflect your situations.

UPDATE:

If you want to customize names and types of your columns you simply have to give them a name:

modelBuilder.Entity<User>()
    .Property(p => p.Id)
    .HasColumnName("user_id")
    .HasColumnType("SMALLINT")
    .IsRequired();

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

whether you are using database first approach ? my user table not having "id" column as int instead am having user_id with different data type and also i don't have userlogin and userclaims table as of now. whether all four tables with all columns are necessary to use identity
can i use the "override OnModelCreating" with 'DB first' approach? (without using edmx, i am creating the models & DbContext classes myself)
@java-love: sure you can. you can create the database and tables yourself and map the structure in OnModelCreating. You must have the default fields definited in the base models, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.