首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Seed方法不播种

Seed方法不播种
EN

Stack Overflow用户
提问于 2016-11-09 20:19:00
回答 1查看 520关注 0票数 1

我的seed方法没有显示任何错误,但是在我从PMC运行update-database之后,它肯定不会播种任何东西。我的问题非常具体,因为数据库的整体结构和身份的使用。到目前为止,Seed方法是这样的:

代码语言:javascript
复制
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类:

代码语言:javascript
复制
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{

    public DbSet<Dentist> Dentists { get; set; }
    public DbSet<Patient> Patients { get; set; }
}

我的模型从ApplicationUser扩展而来,而后者又从IdentityUser扩展而来

ApplicationUser:

代码语言:javascript
复制
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;
    }


}

牙医:

代码语言:javascript
复制
[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; }
}

患者:

代码语言:javascript
复制
[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; }
}

我想知道,为什么它没有给出任何结果,我应该如何看待我建立的这种关系,或者还有其他更符合逻辑的关系可以建立。

EN

回答 1

Stack Overflow用户

发布于 2016-11-09 20:31:28

我通常使用这种方法:

  1. 使用我想要的密码在我的系统中创建一个用户(或使用下面的passwordhaser类,链接如下)
  2. 使用这些值在db
  3. 更新Seed方法中查找用户。

这样,我就不会在Seed方法中使用任何依赖项,因为该方法将在每次迁移时运行,可能需要一段时间才能完成。

Seed方法应该使用AddOrUpdate方法:

代码语言:javascript
复制
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上工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40506882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档