Asp.net 身份实体框架数据库第一种方法与自己的表定义 [英] Asp.net identity entity framework database first approach with own table defintion
问题描述
我有以下四张桌子
角色表
用户表
UserRole 表
用户类型表
具有以下表格的默认 Asp.net 身份,如 Asp.netusers、Asp.netRoles、Asp.netuserlogins、Asp.netuserclaims、Asp.netuserroles
我的表格与相同的列名称不匹配,并且某些列在我的表格中不可用.我可以使用我自己的表来利用 asp.net 身份的功能,否则我需要按照 Asp.netusers 表中使用的相同列到我的用户表中.
是否需要在我的表中添加所有列?
我已经使用相同的默认表实现了 asp.net 身份 EF 数据库第一种方法.我有单独的角色存储和用户存储
这里的上下文用户,用户角色与asp.net身份(asp.netusers,asp.netroles)中的默认表相同
公共部分类 OVT_UserEntities : DbContext{公共 OVT_UserEntities(): base("name=OVT_UserEntities"){}protected override void OnModelCreating(DbModelBuilder modelBuilder){抛出新的 UnintentionalCodeFirstException();}公共虚拟 DbSet用户声明{获取;放;}公共虚拟 DbSet用户登录 { 获取;放;}公共虚拟 DbSet用户角色{获取;放;}公共虚拟 DbSet<用户>用户{得到;放;}}
用户类别:
公共部分类 User : IUser{}
角色类别:
公共部分类 UserRole : IRole{}
现在我想使用上面四个新表(上面的表设计图像)而不是由 asp.net identity 提供的现有表.所以我不确定是否需要在我的数据库中添加相同的所有表和列才能处理 asp.net 身份?
由于您使用的是 Microsoft.AspNet.Identity
,您应该从 IdentityUser
(命名空间)继承您的 UserMicrosoft.AspNet.Identity.EntityFramework
).
你的类应该这样定义:
用户
public class User : IdentityUser{}
角色
public class Role : IdentityRole{}
用户角色
公共类 UserRole : IdentityUserRole{}
用户索赔
公共类 UserClaim : IdentityUserClaim{}
用户登录
公共类 UserLogin : IdentityUserLogin{}
您可以扩展类添加您自己的自定义列:
public class User : IdentityUser{公共字符串 CompanyName { 获取;放;}}
现在您必须定义商店:
public class UserStore: UserStore{公共用户存储(MyContext 上下文):基础(上下文){}}
然后是您的数据库上下文,从 IdentityDbContext
继承:
public class MyContext : IdentityDbContext{public MyContext(): base("ConnectionString"){}}
在您的数据库上下文 (MyContext) 中,您必须覆盖 OnModelCreating
以便您可以创建列、更改类型、表名称等:
protected override void OnModelCreating(DbModelBuilder modelBuilder){base.OnModelCreating(modelBuilder);modelBuilder.Entity().Property(p => p.Id).HasColumnType("int").是必须的();modelBuilder.Entity().Property(p => p.Id).HasColumnType("int").是必须的();modelBuilder.Entity().Property(p => p.RoleId).HasColumnType("int").是必须的();modelBuilder.Entity().Property(p => p.UserId).HasColumnType("int").是必须的();modelBuilder.Entity().Property(p => p.Id).HasColumnType("int").是必须的();modelBuilder.Entity().Property(p => p.UserId).HasColumnType("int").是必须的();modelBuilder.Entity().Property(p => p.UserId).HasColumnType("int").是必须的();modelBuilder.Entity().ToTable("用户");modelBuilder.Entity().ToTable("角色");modelBuilder.Entity().ToTable("用户角色");modelBuilder.Entity().ToTable("用户声明");modelBuilder.Entity().ToTable("用户登录");}
现在您可以使用
I have below four tables
Role table
User table
UserRole table
UserType table
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 ?
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();
这篇关于Asp.net 身份实体框架数据库第一种方法与自己的表定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!