【译】第13节---数据注解-Required

 原文:http://www.entityframeworktutorial.net/code-first/required-attribute-dataannotations-in-code-first.aspx

Required 属性可以应用于域类的属性。 EF Code First将在数据库表中为我们应用Required属性的属性创建一个NOT NULL列。 请注意,它也可以与ASP.Net MVC一起用作验证属性。

请看以下示例:

using System.ComponentModel.DataAnnotations;
    
public class Student
{
    public Student()
    { 
        
    }
    public int StudentID { get; set; }
     
    [Required]
    public string StudentName { get; set; }
        
}

如上面的代码所示,我们将Required属性应用于Student Name。 所以,Code First将在Student表中创建一个NOT NULL Student Name列,如下所示:

原文地址:https://www.cnblogs.com/talentzemin/p/7216450.html