Entity Framework Core Tutorial 数据注释
在.NET Framework中,数据注释通过添加属性标记为数据添加了额外的含义。它用于配置将突出显示最常用配置的类。
- Data Annotations属性是.NET属性,可以应用于实体类或属性以覆盖EF6和EF Core中的默认CodeFirst约定。
- 使用数据注释功能的优点是,通过应用数据属性,我们可以在一个地方管理数据定义,而不需要在多个位置重写相同的规则。
- 它可以在许多.NET应用程序中使用,例如ASP.NET MVC,它允许这些应用程序利用相同的注释进行客户端验证。
最常用的Data Annotations属性如下;
键
您可以使用Key属性将单个属性配置为实体的键。
public class OrderDetail { [Key] public int OrderDetailID { get; set; } public int OrderID { get; set; } public int ProductID { get; set; } public int Quantity { get; set; } public Order Order { get; set; } }
并发令牌(ConcurrencyCheck)
您可以使用ConcurrencyCheck属性将属性配置为并发令牌。
public class Customer { public int CustomerId { get; set; } public string LastName { get; set; } public string FirstName { get; set; } public string Address { get; set; } [ConcurrencyCheck] public string Version { get; set; } }
您可以使用任何要参与并发检查的属性。
NotMapped
您可以使用NotMapped属性从模型或实体的任何属性中排除类型。
[NotMapped] public class BlogMetadata { public DateTime LoadedFromDatabase { get; set; } }
要么
public class Blog { public int BlogId { get; set; } public string Url { get; set; } [NotMapped] public DateTime LoadedFromDatabase { get; set; } }
需要
您可以使用Required属性指示属性是必需的。
public class Blog { public int BlogId { get; set; } [Required] public string Url { get; set; } }
最长长度
您可以使用MaxLength属性配置属性的最大长度。
public class Person { public int PersonId { get; set; } [MaxLength(50)] public string LastName { get; set; } [MaxLength(50)] public string FirstName { get; set; } }
使用MINLENGTH
您可以使用MinLength属性配置属性的最小长度。
public class Person { public int PersonId { get; set; } [MinLength(3)] public string LastName { get; set; } [MinLength(3)] public string FirstName { get; set; } }
StringLength
您可以使用StringLength属性指定其他属性验证,如MaxLength。唯一的区别是StringLength属性只能应用于Domain类的字符串类型属性。
public class Person { public int PersonId { get; set; } [StringLength(50)] public string LastName { get; set; } [StringLength(50)] public string FirstName { get; set; } }
ForeignKey的
您可以使用Data ForeignKey属性来配置应将哪个属性用作给定关系的外键属性。
public class OrderDetail { public int OrderDetailID { get; set; } public int OrderID { get; set; } public int ProductID { get; set; } public int Quantity { get; set; } [ForeignKey("OrderID")] public Order Order { get; set; } } public class Order { public int OrderID { get; set; } public int CustomerID { get; set; } public int EmployeeID { get; set; } public DateTime OrderDate { get; set; } public List<OrderDetail> OrderDetails { get; set; } }
时间戳
您可以使用与ConcurrencyCheck属性相同的Timestamp属性,但它还将确保代码首次生成的数据库字段不可为空。
public class Person { public int PersonId { get; set; } public string LastName { get; set; } public string FirstName { get; set; } [Timestamp] public byte[] TStamp { get; set; } }
表
您可以使用Table属性映射与数据库中的表名不同的类名。
[Table("UserInfo")] public class Person { public int PersonId { get; set; } public string LastName { get; set; } public string FirstName { get; set; } }
柱
您可以使用Column属性映射与数据库中的列名称不同的属性名称。它也与Table属性相同,但Table属性会覆盖表行为,而Column属性会覆盖列行为。
public class Person { public int PersonId { get; set; } [Column("LName")] public string LastName { get; set; } [Column("FName")] public string FirstName { get; set; } }