2016-04-25 85 views
0

我正在使用代碼第一次遷移在MVC 5中開發一個網站。我想添加默認標識中的自定義屬性來更新用戶數據,但我無法更新AspNetUsers表,儘管我也添加了遷移和更新數據庫,但結果相同。我的代碼是 in applicationUser我添加了三個屬性。如何更新身份用戶?

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; 
    } 

} 

然後我更新RegisterViewModel

[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; } 

和註冊View是

<h4>Create a new account.</h4> 
<hr /> 
@Html.ValidationSummary("", new { @class = "text-danger" }) 

<div class="form-group"> 
    @Html.LabelFor(m => m.Name, new { @class = "col-md-2 control-label" }) 
    <div class="col-md-10"> 
     @Html.TextBoxFor(m => m.Name, new { @class = "form-control" }) 
    </div> 
</div> 

<div class="form-group"> 
    @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" }) 
    <div class="col-md-10"> 
     @Html.TextBoxFor(m => m.Email, new { @class = "form-control" }) 
    </div> 
</div> 
<div class="form-group"> 
    @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" }) 
    <div class="col-md-10"> 
     @Html.PasswordFor(m => m.Password, new { @class = "form-control" }) 
    </div> 
</div> 
<div class="form-group"> 
    @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" }) 
    <div class="col-md-10"> 
     @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" }) 
    </div> 
</div> 

<div class="form-group"> 
    @Html.LabelFor(m => m.MobileNo, new { @class = "col-md-2 control-label" }) 
    <div class="col-md-10"> 
     @Html.TextBoxFor(m => m.MobileNo, new { @class = "form-control" }) 
    </div> 
</div> 
<div class="form-group"> 
    @Html.LabelFor(m => m.Address, new { @class = "col-md-2 control-label" }) 
    <div class="col-md-10"> 
     @Html.TextBoxFor(m => m.Address, new { @class = "form-control" }) 
    </div> 
</div> 
<div class="form-group"> 
    <div class="col-md-offset-2 col-md-10"> 
     <input type="submit" class="btn btn-default" value="Register" /> 
    </div> 
</div> 

將在控制檯管理的遷移和更新數據庫之後我無法創建AspNetUsers表中的列和當我使用-Verbose標誌時,錯誤消息是 「找不到對象」AspNetUsers「,因爲它不存在或者您沒有權限離子「。 我無法理解如何解決這個問題。請告訴我如何處理這個問題。謝謝

回答

1

檢查您是否可以實際連接到數據庫,因爲失敗連接可能會阻止您更新數據庫表。我在我的代碼中使用此連接字符串:

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