原文:https://blog.csdn.net/weixin_40719943/article/details/106958701
创建数据库表的时候通常要对相关的列添加约束,例如长度,默认值,格式等,如下代码:
添加约束时需引用using System.ComponentModel.DataAnnotations
public class Club
{
public Club()
{
Players = new List<Player>();
}
public int Id { get; set; }
[Required]
[MaxLength(20)]
public string Name { get; set; }
[DefaultValue("ShangHai")]
public string City { get; set; }
[Column(TypeName = "date")]//限定数据库中只有日期,没有时间的部分
public DateTime DateOfEstablishment { get; set; }
[MaxLength(20)]
public string History { get; set; }
public League League { get; set; }
public List<Player> Players { get; set; }
}