简体   繁体   English

具有自己的表定义的ASP.NET身份实体框架数据库第一种方法

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

I have below four tables 我有以下四个表

Role table 角色表

在此处输入图片说明

User table 用户表

在此处输入图片说明

UserRole table UserRole表

在此处输入图片说明

UserType table UserType表

在此处输入图片说明

default Asp.net identity having below tables like Asp.netusers,Asp.netRoles,Asp.netuserlogins,Asp.netuserclaims,Asp.netuserroles 默认的Asp.net身份具有以下表格,如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. 我可以使用自己的表来利用asp.net身份的功能吗,否则我需要将Asp.netusers表中使用的相同列应用于User表。

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. 我已经使用相同的默认表实现了asp.net身份EF数据库优先方法。 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) 在下面的上下文中,用户,userroles是与asp.net身份相同的默认表(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. 现在我想使用上面的四个新表(上面的表设计图像),而不是使用asp.net身份提供的现有表。 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 ? 所以我不确定是否需要在数据库中添加相同的所有表和列才能使用asp.net身份?

Since you're using Microsoft.AspNet.Identity you should inherit your User from IdentityUser (namespace Microsoft.AspNet.Identity.EntityFramework ). 由于您使用的是Microsoft.AspNet.Identity ,因此应该从IdentityUser (命名空间Microsoft.AspNet.Identity.EntityFramework )继承您的User。

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 : 然后是您的数据库上下文,继承自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: 在数据库上下文( MyContext )中,您必须重写OnModelCreating以便可以创建列,更改类型,表名称等:

    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. 我更新了一个github 项目来反映您的情况。

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM