模型验证规则

数据库自动获取时间 

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]

Required 注释告诉 EF 某一个特定属性是必需的。

 

[Required]
public string Title { get; set; }

声明字段长度

(1)MaxLength和MinLength     

 

      我们也经常会在数据库中限定特定字段的字符串长度,以方便日后创建字段索引,可以使用MaxLength属性,引用System.ComponentModel.DataAnnotations。

 

[MaxLength(10), MinLength(5)]
public string BloggerName { get; set; }

 

      也可以在注释中指定ErrorMessage。

 

[MaxLength(10,  ErrorMessage="BloggerName 必须在 10 个字符以下"), MinLength(5)]
public string BloggerName { get; set; }

 

 

(2)StringLength      

 

[StringLength(50, MinimumLength=1)]
public string LastName { get; set; }

 

posted on 2016-01-05 11:40  the_best  阅读(325)  评论(0)    收藏  举报