2

I want to add 2 Extra fields in UserProfile Asp.Net Simple membership, But unable to add and not find any help from internet. Please give me some solutions.

Thanks in advance

Waqas Idrees
  • 1,443
  • 2
  • 17
  • 36
  • 1
    possible duplicate of [How do I add an extra field using ASP.Net membership provider?](http://stackoverflow.com/questions/3435812/how-do-i-add-an-extra-field-using-asp-net-membership-provider) – Rumit Parakhiya Oct 21 '13 at 12:26
  • Show what you have tried so far. There are dozen of posts about adding fields to the User Profile table. https://www.google.co.uk/search?q=asp.net+mvc4+simplemembership+add+field&ie=utf-8&oe=utf-8&rls=org.mozilla:en-US:official&client=firefox-a&gws_rd=cr&ei=eh1lUt6fFfO00QXNwoGICg – Lotok Oct 21 '13 at 12:26
  • Dear I am taking about Simple Membership which is newly add in MVC 4 not Asp.Net Membership Provider Thanks – Waqas Idrees Oct 21 '13 at 12:43

1 Answers1

1

To add fields to UserProfile in SimpleMembership (I will assume you want to use migrations and EF):

  1. Ensure you have enabled migrations (use the Enable-Migration in the package manager console)
  2. Edit the UserProfile class (assuming you are using, for example, the Visual Studio 2012 -> New Project -> ASP.NET MVC 4 Web Application -> Internet Application template, this will be created for you) to add your new properties (example below)
  3. Add a migration which will update your database using the Add-Migration command.
  4. Optionally edit the generated migration to handle default values and non-nullable fields.
  5. Run that migration against your development database using the Update-Database command.

I have provided an overview about how SimpleMembership works, with UserProfile in this answer. Note that UserProfile can be moved from the UsersContext to the same DbContext as all your other classes, it does not have to be in a separate context.

SimpleMembership is designed to play well with code-first EF development, so that is the process I have outlined above. An example updated UserProfile class with a Forename, Surname and a LoginCount field would look like:

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }

    public string UserName { get; set; }

    [MaxLength(40)]
    [Required]
    public string Forename { get; set; }

    [MaxLength(40)]
    [Required]
    public string Surname { get; set; }

    [Required]
    public int LoginCount { get; set; }
}

References:

  1. ASP.NET MVC 4 Entity Framework Scaffolding and Migrations - task 3 shows how to enable migrations
  2. MSDN: Code First Migrations - everything you need to know about migrations
  3. StackOverflow: How do I use my own database with SimpleMembership and WebSecurity? What is MVC4 security all about?
Community
  • 1
  • 1
Andy Brown
  • 18,961
  • 3
  • 52
  • 62