Skip to content

Instantly share code, notes, and snippets.

@jwill9999
Created April 5, 2017 00:26
Show Gist options
  • Save jwill9999/52c327c95ab885b8a4ce377c2bf1bb77 to your computer and use it in GitHub Desktop.
Save jwill9999/52c327c95ab885b8a4ce377c2bf1bb77 to your computer and use it in GitHub Desktop.
Adding Validation to Asp.net Mvc5 entity framework code first approach
****example******
public class Movie
{
    public int ID { get; set; }
    [StringLength(60, MinimumLength = 3)]
    public string Title { get; set; }
    [Display(Name = "Release Date")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime ReleaseDate { get; set; }
    [RegularExpression(@"^[A-Z]+[a-zA-Z''-'\s]*$")]
    [Required]
    [StringLength(30)]
    public string Genre { get; set; }
    [Range(1, 100)]
    [DataType(DataType.Currency)]
    public decimal Price { get; set; }
    [RegularExpression(@"^[A-Z]+[a-zA-Z''-'\s]*$")]
    [StringLength(5)]
    public string Rating { get; set; }
}
*****Instructions*******
Build solution then
add-migration DataAnnotations
update-database
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment