Asp.net identity entity framework database first approach with own table defintion

前端 未结 1 835
暗喜
暗喜 2020-12-07 00:11

I have below four tables

Role table

User table

相关标签:
1条回答
  • 2020-12-07 00:39

    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();
    

    0 讨论(0)
提交回复
热议问题