个人资料

跳过导航链接首页 > 博客列表 > 博客正文

EF Core中数据库字段的Data annotations修饰符

:
[Table("Blogs")]
public class Blog
{
    public int BlogId { get; set; }

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

数据库表名

[Comment("Blogs managed on the website")]

表注释 在class前 也可在属性前 表示对数据库列注释

[NotMapped]

在属性前,表示属性不存储到到数据库

[Column("blog_id")]

列名,自定义数据库中的列名.

[Column(TypeName = "decimal(5, 2)")]    [Column(TypeName = "varchar(200)")]

同时可设置类型 5,2表示5位整数2位小数

[MaxLength(500)]  对于string [StringLength(50, MinimumLength = 3)]

限定属性在数据库中存储的最大长度,优化性能

[Precision(14, 2)]

精确存储小数

[Unicode(false)]

字符串编码,默认位unicode

[Required]

必须字段.非必需字段建议使用可空类型,如string?

[Column(Order = 0)]

在属性前 手动指定数据列顺序

[Key]

指定属性为表的主键

[NotNull]  [AllowNull]

不为空

[Display(Name = "说明")]

[DataType(DataType.Date)

[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]

前端的属性说明,限定格式

[ForeignKey("puuid")]

指定连接到puuid属性作为外键


songshizhao
最初发表2025/1/13 20:20:03 最近更新2025/1/14 9:29:45 49
为此篇作品打分
10