我有一个为数据库设定种子的类,它为2个用户添加了角色和自定义字段。我遇到的问题是它将数据保存在[dbo]。[AspNetUsers]而不是[dbo]。[IdentityUsers]。两个表都已创建。播种时,数据进入AspNetUser。当我启动网站并注册新用户时,数据将进入IdentityUser。
这是Migration类:
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");
}
}
这是ApplicationUser类,它定义Identity模型的自定义字段。
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);
}
}
两者都使用具有相同配置的save DataContext:
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应用程序使用另一个时使用一个?
答案 0 :(得分:6)
您的DbContext是否继承自IdentityDbContext?
如果从IdentityDbContext继承,则不需要在OnModelCreating中进行任何其他配置。 IdentityDbContext和默认的实体框架约定提供的映射应该足够了。
如果您在没有UserId的setter的情况下声明ApplicationUser,则无需忽略UserId属性:
public class ApplicationUser : IdentityUser
{
public ApplicationUser()
{
Email = "";
Language = "";
}
public string Email { get; set; }
public string Language { get; set; }
public string UserId
{
get { return Id; }
}
}
然后你的ApplicationDbContext可以像这样简单(为了清楚起见,我已将Configuration类重命名为MigrationConfiguration):
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
static ApplicationDbContext()
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, MigrationConfiguration>());
}
public ApplicationDbContext()
: base("DefaultConnection")
{
}
}
这与AspNet.Identity.Core和AspNet.Identity.EntityFramework程序集的发行版本(版本1.0.0)一样按预期工作(只有名为AspNetXXXX的表,AspNetUsers表具有自定义字段)。