Muhammad Abdullah
Muhammad Abdullah

Reputation: 1

how to update the Identity User?

I'm developing a website in MVC 5 using code first migration. I want to add the custom attributes in default identity to update the users data but I failed to update the AspNetUsers table although I have added migration and update database too but the result is the same. my code is in applicationUser I have add three more properties.

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

}

then I updated 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; }

and Register View is

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

after adding the migration and update database in console manager I am unable to create the columns in AspNetUsers table and when I use the -Verbose flag then the error message was "Cannot find the object "AspNetUsers" because it does not exist or you do not have permissions." I'm unable to understand that how to fix the problem. Please tell me how to deal with that problem. Thanks

Upvotes: 0

Views: 88

Answers (1)

Shawn Yan
Shawn Yan

Reputation: 183

Check whether you can actually connect to your database as fail connection might prevent you from updating the database table. I am using this connection string in my code :

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

Upvotes: 1

Related Questions