这是我的model类:
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; }
}
public class MovieDBContext:DbContext
{
public DbSet<Movie> Movies { get; set; }
}
我添加了许多限定,如何将其迁移到数据库
首先打开程序包管理控制台,输入命令:add-migration DataAnnotations //迁移数据库的限定
update-database //更新
即可。
需要注意的是在model类中添加的限定可以运用到程序任意角落,很好的体现了MVC的DRY原则。