1 Data Annotation Attributes
这些.NET特性能被应用到领域类的属性中,这些特性包含在单独的命名空间中System.ComponentModel.DataAnnotations
在下面代码中我们给到2个领域类的属性提交Data Annotation Attributes (Country & City)
[Table("TBL_City")]public class City{[Key]public int KeyId { get; set; }[Column("CityName", TypeName = "varchar(25)")]public string Name { get; set; }[NotMapped]public int Population { get; set; }[ForeignKey("FKid")]public Country Country { get; set; }}[Table("TBL_Country")]public class Country{[Key]public int KeyId { get; set; }[MaxLength(20)]public string Name { get; set; }}
在视图的输入项中我们广泛应用了Data Annotation Attributes
TBL_Country 表:
下面图片显示了数据库中两张表
EF Core Data Annotations
| 特性 | 描述 |
|---|---|
| Table | 实体类映射到数据库中表的名称 |
| Column |
属性映射到在表的列名称、顺序和数据类型 |
| Key | 设置属性作为表的主键 |
| ForeignKey | 将一个属性标记为外键 |
| NotMapped | 不会在数据库的表中生成该属性对应的列 |
| MaxLength | 为表列设置最大值 |
| Required | 指定该属性在表中对应的列不能为空 |
2 Fluent API
可以使用EF Core Fluent API配置领域类,接下我们会写4篇文章来了解Fluent API
总结