如何更新身份用户?

时间:2016-04-25 15:18:10

标签: asp.net-mvc-5

我正在使用代码优先迁移在MVC 5中开发一个网站。我想在默认身份中添加自定义属性以更新用户数据,但我无法更新AspNetUsers表,尽管我已经添加了迁移和更新数据库,但结果是相同的。我的代码是 在applicationUser中我添加了三个属性。

show()

然后我更新了RegisterViewModel

public class ApplicationUser : IdentityUser
{
    public string Name { get; set; }
    public string MobileNo { get; set; }
    public string Address { 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;
    }

}

和注册视图

[Required]
    [Display(Name ="Name")]
    public string Name { get; set; }

    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    [Required]
    [Display(Name = "Mobile Number")]
    public string MobileNo { get; set; }

    [Required]
    [Display(Name = "Address")]
    public string Address { get; set; }

在控制台管理器中添加迁移和更新数据库后,我无法在AspNetUsers表中创建列,当我使用-Verbose标志时,错误消息是 &#34;找不到对象&#34; AspNetUsers&#34;因为它不存在或您没有权限。&#34; 我无法理解如何解决问题。请告诉我如何处理这个问题。感谢

1 个答案:

答案 0 :(得分:1)

检查是否可以实际连接到数据库,因为失败连接可能会阻止您更新数据库表。我在我的代码中使用此连接字符串:

<connectionStrings> <add name="CONNECTIONSTRING_NAME" connectionString="Data Source(local); Initial Catalog=YOUR_DATABASE_NAME;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>