我的种子方法没有显示任何错误,但是在我从PMC运行update-database之后肯定没有播种任何内容。我的问题非常具体,因为数据库的整体结构和身份的使用。这就是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();
}
}
我的ApplicationDbContext类:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Dentist> Dentists { get; set; }
public DbSet<Patient> Patients { get; set; }
}
我的模型从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; }
}
我想知道,为什么它没有给出任何结果,我应该如何看待我所建立的这种关系,或者是否还有其他更合理的关系。
答案 0 :(得分:1)
我通常使用这种方法:
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())
仅在该表中没有记录时才能用于新数据库。