【发布时间】:2013-12-24 01:18:05
【问题描述】:
我有一个类为数据库添加 2 个具有角色和自定义字段的用户。我遇到的问题是它将数据保存在 [dbo].[AspNetUsers] 而不是 [dbo].[IdentityUsers] 中。两个表都已创建。播种后,数据进入 AspNetUser。当我启动网站并注册新用户时,数据会进入 IdentityUser。
这是迁移类:
internal sealed class Configuration : DbMigrationsConfiguration<DatabaseContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(DatabaseContext context)
{
base.Seed(context);
var userStore = new UserStore<ApplicationUser>();
var manager = new UserManager<ApplicationUser>(userStore);
var role = new IdentityUserRole { Role = new IdentityRole(Model.Roles.ADMINISTRATOR) };
var user = new ApplicationUser() { UserName = "123123", Email = "123123@123.com", Language = "en-US"};
user.Roles.Add(role);
IdentityResult result = manager.Create(user, "123123");
var role2 = new IdentityUserRole { Role = new IdentityRole(Model.Roles.NORMAL) };
var user2 = new ApplicationUser() { UserName = "qweqwe", Email = "qweqwe@qweqwe.com", Language = "fr-CA" };
user.Roles.Add(role2);
IdentityResult result2 = manager.Create(user2, "qweqwe");
}
}
这是为 Identity 模型定义自定义字段的 ApplicationUser 类。
public class ApplicationUser : IdentityUser, ICurrentUser
{
public ApplicationUser()
{
Email = "";
Language = "";
}
public string UserId {
get { return base.Id; }
set{}
}
public string Email { get; set; }
public string Language { get; set; }
}
这是这个新类的实体框架配置类。
public class ApplicationUserConfiguration : EntityTypeConfiguration<ApplicationUser>
{
public ApplicationUserConfiguration()
{
this.HasKey(d => d.Id);
this.Ignore(d => d.UserId);
}
}
两者都使用相同配置的保存数据上下文:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//... others entity here
modelBuilder.Configurations.Add(new ApplicationUserConfiguration());
//Theses configuration are required since custom fields are added to ApplicationUser. Here is why : http://stackoverflow.com/questions/19474662/map-tables-using-fluent-api-in-asp-net-mvc5-ef6
modelBuilder.Entity<IdentityUserLogin>().HasKey(l => l.UserId);
modelBuilder.Entity<IdentityRole>().HasKey(r => r.Id);
modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
}
我的问题是:为什么我的 AspNet 前缀表名称与身份表重复?为什么种子使用一个而 Web 应用程序使用另一个?
【问题讨论】:
标签: asp.net-mvc entity-framework entity-framework-6 asp.net-identity