必需属性

应用于域类的属性时,数据库将创建 NOT NULL 列。

using System.ComponentModel.DataAnnotations;

public class Person
{
    public int PersonID { get; set; }
    
    [Required]
    public string PersonName { get; set; }    
}

生成的列具有 NOT NULL 约束:

StackOverflow 文档

注意: 它也可以与 asp.net-mvc 一起用作验证属性。