原文地址:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx
EF 6 Code-First系列文章目錄:
- 1 翻譯系列:什么是Code First(EF 6 Code First 系列)
- 2.翻譯系列:為EF Code-First設置開發環境(EF 6 Code-First系列)
- 3.翻譯系列:EF Code-First 示例(EF 6 Code-First系列)
- 4.翻譯系列:EF 6 Code-First默認約定(EF 6 Code-First系列)
- 5.翻譯系列:EF 6中數據庫的初始化(EF 6 Code-First 系列)
- 6.翻譯系列:EF 6 Code-First中數據庫初始化策略(EF 6 Code-First系列
- 7.翻譯系列:EF 6中的繼承策略(EF 6 Code-First 系列)
- 8.翻譯系列: EF 6中配置領域類(EF 6 Code-First 系列)
- 9.翻譯系列:EF 6以及EF Core中的數據注解特性(EF 6 Code-First系列)
- 9.1 翻譯系列:數據注解特性之----Table【EF 6 Code-First 系列】
- 9.2 翻譯系列:數據注解特性之---Column【EF 6 Code First系列】
- 9.3 翻譯系列:數據注解特性之Key【EF 6 Code-First 系列】
- 9.4 翻譯系列:EF 6以及 EF Core中的NotMapped特性(EF 6 Code-First系列)
- 9.5 翻譯系列:數據注解之ForeignKey特性【EF 6 Code-First系列】
- 9.6 翻譯系列:數據注解之Index特性【EF 6 Code-First系列】
- 9.7 翻譯系列:EF數據注解特性之--InverseProperty【EF 6 Code-First系列】
- 9.8 翻譯系列:數據注解特性之--Required 【EF 6 Code-First系列】
- 9.9 翻譯系列:數據注解特性之--MaxLength 【EF 6 Code-First系列】
- 9.10 翻譯系列:EF數據注解特性之StringLength【EF 6 Code-First系列】
- 9.11 翻譯系列:數據注解特性之--Timestamp【EF 6 Code-First系列】
- 9.12 翻譯系列:數據注解特性之ConcurrencyCheck【EF 6 Code-First系列】
- 10.翻譯系列:EF 6中的Fluent API配置【EF 6 Code-First系列】
- 10.1.翻譯系列:EF 6中的實體映射【EF 6 Code-First系列】
- 10.2.翻譯系列:使用Fluent API進行屬性映射【EF 6 Code-First】
- 11.翻譯系列:在EF 6中配置一對零或者一對一的關系【EF 6 Code-First系列】
- 12.翻譯系列:EF 6 中配置一對多的關系【EF 6 Code-First系列】
- 13.翻譯系列:Code-First方式配置多對多關系【EF 6 Code-First系列】
- 14.翻譯系列:從已經存在的數據庫中生成上下文類和實體類【EF 6 Code-First系列】
- 15.翻譯系列:EF 6中的級聯刪除【EF 6 Code-First 系列】
- 16.翻譯系列:EF 6 Code -First中使用存儲過程【EF 6 Code-First系列】
- 17.翻譯系列:將Fluent API的配置遷移到單獨的類中【EF 6 Code-First系列】
- 18.翻譯系列:EF 6 Code-First 中的Seed Data(種子數據或原始測試數據)【EF 6 Code-First系列】
- 19.翻譯系列:EF 6中定義自定義的約定【EF 6 Code-First約定】
- 20.翻譯系列:Code-First中的數據庫遷移技術【EF 6 Code-First系列】
- 20.1翻譯系列:EF 6中自動數據遷移技術【EF 6 Code-First系列】
- 20.2.翻譯系列:EF 6中基於代碼的數據庫遷移技術【EF 6 Code-First系列】
- 21.翻譯系列:Entity Framework 6 Power Tools【EF 6 Code-First系列】
數據注解特性是.NET特性,可以在EF或者EF Core中,應用於實體類上或者屬性上,以重寫默認的約定規則。
在EF 6和EF Core中,數據注解特性包含在System.ComponentModel.DataAnnotations命名空間和System.ComponentModel.DataAnnotations.Schema命名空間下。
這些特性不僅僅適用於EF,同樣適用於ASP.NET MVC以及數據控件。數據注解特性,在EF 6和EFCore中工作方式都是相同的、效用一樣的。
System.ComponentModel.DataAnnotations Attributes
特性 | 描述 |
Key | 應用於實體的一個屬性上,然后就生成表里面的主鍵 |
Timestamp | 應用於實體的一個屬性上,然后生成表的列的類型就是RowVersion |
ConcurrencyCheck | 應用的屬性上,然后相應的列就會生成一個樂觀檢查 |
Required | 應用的屬性上面,生成一個不為空的列,然后映射到數據庫的列中 |
MinLength | 定義最小字符串長度,然后映射到數據庫的列中 |
MaxLength | 定義最大字符串長度,然后映射到數據庫的列中 |
StringLength | 可以指定屬性列允許的最大字符串長度,然后映射到數據庫的列中 |
System.ComponentModel.DataAnnotations.Schema Attributes
特性 | 描述 |
Table | 應用於實體,可以配置表名和模式名 |
Column | 應用於實體的屬性上,可以配置相應的列名,列順序以及數據類型 |
Index | 應用於實體的屬性上,可以為列生成索引(EF 6.1以上的版本才支持) |
ForeignKey | 應用於實體的屬性上,然后生成外鍵屬性 |
NotMapped | 可以應用於實體或者實體的屬性上,應用於實體上,就不會生成表,應用於屬性上,就不會生成列 |
DatabaseGenerated | 配置列自動生成,可以有三個選項:identity【自增】, computed【計算】 or none【無】 |
InverseProperty | 應用於屬性上。就是有相同的關系的時候,用於反轉導航屬性。【概念不好說,等會后面有例子講解。】 |
ComplexType | 在EF 6中標記類是復雜類型,注意:EF Core 2.0 中不支持這個Complex特性 |
我們在后面的子節中,來好好學習一個每個特性。