У меня есть класс, который заселяет базу данных, которая добавляет 2 пользователя с ролями и настраиваемыми полями. Проблема заключается в том, что она сохраняет данные в [dbo]. [AspNetUsers] вместо [dbo]. [IdentityUsers]. Создаются обе таблицы. При посеве данные поступают в AspNetUser. Когда я запускаю веб-сайт и регистрирую нового пользователя, данные переходят в IdentityUser.Entity Framework Seeding with Identity (Microsoft.Owin.Security) user
Вот класс миграции:
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 = "[email protected]", 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 = "[email protected]", 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; }
}
Здесь приведен класс конфигурации Entity Framework для этого нового класса.
public class ApplicationUserConfiguration : EntityTypeConfiguration<ApplicationUser>
{
public ApplicationUserConfiguration()
{
this.HasKey(d => d.Id);
this.Ignore(d => d.UserId);
}
}
Оба используют сохранения 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 prefixe, который дублируется из таблиц идентичности? Почему семена используют одно, а веб-приложение использует другое?
Спасибо. Я сделал это с IdentityContext, и все сработало. –