0

The question I had was how are models supposed to be used? I don't think I am using them correctly. Whenever I make a change to the database (user table, in this case) and rebuild the model, my changes get erased and I end up making the changes again. The changes made are adding attributes and also another column(ConfirmPassword).

Edit: I am using EF 5

This is the model that was created by the database:

using System;
using System.Collections.Generic;

public partial class User
{
    public User()
    {
        this.Documents = new HashSet<Document>();
    }

    public int UserId { get; set; }
    public string Username { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string Grade { get; set; }
}

With my changes:

using System;
using System.Collections.Generic;
using CompareObsolete = System.Web.Mvc.CompareAttribute;

public partial class User
{
    public User()
    {
        this.Documents = new HashSet<Document>();
    }

    [Key]
    public int UserId { get; set; }

    [Required]
    [StringLength(15, MinimumLength = 3)]
    [Display(Name = "Username*: ")]
    public string Username { get; set; }

    [Required]
    [EmailAddress]
    [StringLength(25)]
    [Display(Name = "Email Address*: ")]
    public string Email { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [StringLength(50, MinimumLength = 4)]
    [Display(Name = "Password*: ")]
    public string Password { get; set; }

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

    [Required]
    [Display(Name = "Your Grade*: ")]
    public string Grade { get; set; }
}
Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62
User456789
  • 397
  • 1
  • 3
  • 16
  • Are you using Entity Framework? If so which version? – Ryan Pedersen Mar 15 '15 at 03:40
  • Create a separate view model ([What is a viewmodel in mvc](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc)). A property such as `ConfirmPassword` does not belong in your data model. –  Mar 15 '15 at 06:50

1 Answers1

0

1. Create UserMetaData Class

    public class UserMetaData
    {
      [Key]
      public int UserId { get; set; }

      [Required]
      [StringLength(15, MinimumLength = 3)]
      [Display(Name = "Username*: ")]
      public string Username { get; set; }

      [Required]
      [EmailAddress]
      [StringLength(25)]
      [Display(Name = "Email Address*: ")]
      public string Email { get; set; }

      [Required]
      [DataType(DataType.Password)]
      [StringLength(50, MinimumLength = 4)]
      [Display(Name = "Password*: ")]
      public string Password { get; set; }

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

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

2. Create new partial class in new .cs file and assign metadata class to there by using MetadataType attribute to it.

    [MetadataType(typeof(UserMetaData))] // assign type of your metadata here.
    public partial class User
    {

    }
Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62