我的seed方法没有显示任何错误,但在我从PMC运行update-database之后,它肯定没有播种任何东西。由于数据库的整体结构和Identity的使用,我的问题非常具体。到目前为止,Seed方法看起来是这样的:
protected override void Seed(ApplicationDbContext context)
{
if (!context.Users.Any())
{
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
var user = new ApplicationUser()
{
FirstName = "Prvi",
LastName = "Doktor",
DateOfBirth = new DateTime(1977, 4, 3),
EmploymentStatus = EmploymentStatus.Employed,
PhoneNumber = "062/062-062",
Email = "prvi@gmail.ocami",
Address = "Kulovica 9",
DateCreated = DateTime.Now,
EmailConfirmed = true,
};
userManager.Create(user, "P@ssw0rd");
context.SaveChanges();
}
}
My ApplicationDbContext类:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Dentist> Dentists { get; set; }
public DbSet<Patient> Patients { get; set; }
}
我的模型从ApplicationUser扩展,而ApplicationUser又从IdentityUser扩展
ApplicationUser:
public class ApplicationUser : IdentityUser, IModificationHistory
{
[Required]
[Display(Name = "First Name")]
[StringLength(50)]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
[StringLength(50)]
public string LastName { get; set; }
[Required]
[DataType(DataType.Password)] //DataType is very powerfull Data Annotation, which can affect our view if we use EF, so I will try to accomplish as much as possible with that
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name = "Date of birth")]
public DateTime DateOfBirth { get; set; }
[Display(Name = "Address")]
public string Address { get; set; }
public EmploymentStatus? EmploymentStatus { get; set; } //This value is nullable
public DateTime DateCreated { get; set; }
public DateTime? DateModified { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
牙医:
[Table("Dentist")]
public class Dentist : ApplicationUser
{
public string Place { get; set; }
//Relations
public virtual ICollection<Patient> Patients { get; set; }
public virtual Schedule Schedule { get; set; }
}
病人:
[Table("Patient")]
public class Patient : ApplicationUser
{
//Relations
public virtual Dentist Dentist { get; set; }
public virtual MedicalHistory MedicalHistory { get; set; }
public virtual ICollection<Appointment> Appointments { get; set; }
}
我想知道,为什么它没有给出任何结果,我应该如何看待我所做的这个关系,或者是否有任何其他更合乎逻辑的关系。
我通常使用这种方法:
- 在我的系统中使用我想要的密码在注册屏幕上创建一个用户(或使用passwordaser类,链接如下)
- 查找db 中的用户
- 用这些值更新
Seed
方法
这样我就不会在Seed
方法中使用任何依赖项,因为该方法将在每次迁移中运行,并且可能需要一段时间才能完成。
Seed
法应采用AddOrUpdate
法:
protected override void Seed(BookService.Models.BookServiceContext context)
{
context.Users.AddOrUpdate(x => x.Id,
new ApplicationUser () { Id = 1, FirstName = "FirstName", LastName="LastName", PasswordHash="<hash from dbase>" }
);
}
要生成密码的散列,还可以使用PasswordHasher。HashPassword方法。
这样EF就知道何时添加或更新您在Seed
方法中提供的值。
if (!context.Users.Any())
只在没有记录的新数据库上工作。