在asp.net身份表中使用种子插入用户

时间:2016-12-28 08:05:31

标签: c# asp.net asp.net-mvc unit-testing

我正在尝试从代码级别向asp.net身份表结构插入记录,如下所示。

public class ProjectName_DbInitalize : DropCreateDatabaseIfModelChanges<ProjectName_DBContext>
{
    protected override void Seed(ProjectName_DBContext context)
    {

        context.AspNetUsers.Add
        (
              new AspNetUser
              {
                  Email = "kez@gmail.com",
                  EmailConfirmed = true,
                  PasswordHash = "123456",
                  SecurityStamp = "Author 1st Bio",
                  PhoneNumber = "0734248148",
                  PhoneNumberConfirmed = true,
                  LockoutEndDateUtc = null,
                  LockoutEnabled = true,
                  AccessFailedCount = 0,
                  UserName = "",
                  FirstName = "",
                  LastName = "",
                  CreatedBy = "",
                  CreatedDate = null,
                  UpdatedBy = null,
                  UpdatedDate = null

              }
          );

        context.SaveChanges();

        base.Seed(context);

    }

}

在单元测试项目中,我插入如下

public class IdentityRepositoryTest
{
    IdentityRepository identitityRepo;

    // initialize the test class
    [TestInitialize]
    public void TestSetup()
    {
        ProjectName_DbInitalize db = new ProjectName_DbInitalize();
        System.Data.Entity.Database.SetInitializer(db);
        identitityRepo = new IdentityRepository();
    }      
}

但是当我运行它时,它没有向AspNet.Users表或任何其他表插入值,我该如何正确地执行此操作

1 个答案:

答案 0 :(得分:0)

尝试不添加PasswordHash(我认为它是为你做的..因为它需要做哈希并加盐)然后尝试放一个非空用户名(mayb将其设置为电子邮件)。

最后尝试把它放在try catch中,这样你就可以调试它并读出类似这样的错误

public class ProjectName_DbInitalize:DropCreateDatabaseIfModelChanges     {         protected override void Seed(ProjectName_DBContext context)         {

        try
        {
            context.AspNetUsers.Add
                   (
                         new AspNetUser
                         {
                             Email = "kez@gmail.com",
                             EmailConfirmed = true,
                             PasswordHash = "123456",
                             SecurityStamp = "Author 1st Bio",
                             PhoneNumber = "0734248148",
                             PhoneNumberConfirmed = true,
                             LockoutEndDateUtc = null,
                             LockoutEnabled = true,
                             AccessFailedCount = 0,
                             UserName = "",
                             FirstName = "",
                             LastName = "",
                             CreatedBy = "",
                             CreatedDate = null,
                             UpdatedBy = null,
                             UpdatedDate = null

                         }
                     );

            context.SaveChanges();

            base.Seed(context);

            //context.SaveChanges(); 


        }
        catch (Exception ex)
        {

            throw ex;
        }
    }

}