框架中提供了多种数据注释以便可以全面的描述数据库结构特性。
自增列
可以使用注释声明指定列是数据库自增列,同时能指定自增的起始及步长。
public class Blog { [Identity(, )] public int BlogId { get; set; } public string Url { get; set; } }
表映射
表映射标识该从数据库中哪些表数据查询或保存数据。
[Table("blogs")] public class Blog { public int BlogId { get; set; } public string Url { get; set; } }
列映射
列映射标识应该从数据库的哪些列查询或保存数据。
public class Blog { [Column("blog_id")] public int BlogId { get; set; } public string Url { get; set; } }
数据类型
数据类型是指属性映射到的列的数据库特定类型。
public class Blog { public int BlogId { get; set; } [Column(nameof(Url), "varchar(200)")] public string Url { get; set; } [Column(nameof(Rating), "decimal(5, 2)")] public decimal Rating { get; set; } }
计算列
计算列是其值在数据库中计算的列。计算列可以使用表中的其他列来计算其值。
public class Person { public int PersonId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } [Computed("[LastName] + ', ' + [FirstName]")] public string DisplayName { get; set; } }
默认值
列的默认值是插入新行但未为该列指定值时将插入的值。
public class Blog { public int BlogId { get; set; } public string Url { get; set; } [Default(")] public int Rating { get; set; } }
长度
配置长度向数据存储提供关于用于给定属性的适当数据类型的提示。最大长度仅适用于数组数据类型,例如byte[]
。
public class Blog { public int BlogId { get; set; } [Length(, false)] public byte[] Image { get; set; } }
字符串
用于配置字符串的类型信息。
public class Blog { public int BlogId { get; set; } [String(, false)] public string Url { get; set; } }
精度
用于配置存精度信息的数据类型列,例如decimal。
public class Product { public int Id { get; set; } [Precision(,)] public decimal Price { get; set; } }