Asp.net 身份 entity framework 数据库优先方法与自己的 table 定义

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

我有以下四个table

作用table

用户table

用户角色table

用户类型table

默认 Asp.net 身份具有以下 table 个身份,例如 Asp.net 用户、Asp.net 角色、Asp.net 用户登录、Asp.net 用户声明、Asp.net用户角色

我的 table 与相同的列名称不匹配,并且某些列在我的 table 中不可用。我可以使用我自己的 table 来利用 asp.net 身份的功能,否则我需要遵循 Asp.net 用户 table 中使用的相同列到我的用户 table.

是否需要在我的 table 中添加所有列?

我已经实施了 asp.net 身份 EF 数据库优先方法,具有相同的默认 tables。我有单独的角色存储和用户存储

上下文用户,用户角色与 asp.net 中的默认相同 table 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; }
    }

用户class:

public partial class User : IUser<int>
    {
    }

角色class:

public partial class UserRole : IRole<int>
    {
    }

现在我想使用以上四个新的 tables(table 上面的设计图像)而不是 asp.net 身份提供的现有 table。所以我不确定是否需要在我的数据库中添加相同的所有 table 和列才能处理 asp.net 身份?

由于您使用的是 Microsoft.AspNet.Identity,因此您应该从 IdentityUser(命名空间 Microsoft.AspNet.Identity.EntityFramework)继承您的用户。

你的 类 应该这样定义:

用户

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

角色

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

用户角色

public class UserRole : IdentityUserRole<int>
{
}

用户声明

public class UserClaim : IdentityUserClaim<int>
{
}

用户登录

public class UserLogin : IdentityUserLogin<int>
{
}

您可以扩展 类 添加您自己的自定义列:

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

现在您必须定义商店:

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

然后是您的数据库上下文,继承自 IdentityDbContext:

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

    }
}

在您的数据库上下文 (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");

    }

现在您可以使用 migrations to generate 您的表格了。

我更新了 github project 以反映您的情况。

更新:

如果您想自定义列的名称和类型,您只需为它们命名:

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