我的seed方法没有显示任何错误,但是在我从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; }
}我想知道,为什么它没有给出任何结果,我应该如何看待我建立的这种关系,或者还有其他更符合逻辑的关系可以建立。
发布于 2016-11-09 20:31:28
我通常使用这种方法:
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())将仅在新db上工作。
https://stackoverflow.com/questions/40506882
复制相似问题