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系列】
當我們不想實體類中的某個或者某些屬性,不要映射成數據庫中的列的時候。可以使用NotMapped特性,標識NotMapped特性在屬性上面就行了。默認情況下,EF為實體的每個屬性映射數據列。【必須包含get;和set;】。NotMapped特性重寫了這個約定。
NotMapped Attribute: [NotMapped()]

在上面的例子中,NotMapped特性應用在Student實體的Age屬性上了,所以EF將不會在Students表中包含Age列:

需要注意的是:EF不會為沒有get;和set;的屬性,創建列。例如下面實體中的City和Age屬性,都不會映射在數據庫表的列中:

我們自己動手驗證一下:
1.創建一個控制台應用程序EFAnnotationNotMapped,並安裝EF

2.創建一個Student類
public class Student {
[Key] public int Key { get; set; } public string Name { get; set; } [NotMapped] public string Sex { get; set; } public int Age { get; set; } }
3.上下文類:
public class EFDbContext:DbContext { public EFDbContext() : base("name=Constr") { Database.SetInitializer<EFDbContext>(new DropCreateDatabaseAlways<EFDbContext>()); } public DbSet<Student> Students { get; set; } }
4.配置文件:
<connectionStrings>
<add name="Constr" connectionString="server=.;database=EFAnnotationNotMappedDB;uid=sa;pwd=Password_1" providerName="System.Data.SqlClient"/>
</connectionStrings>
5.測試代碼:

6.運行程序:

看看生成的數據庫:

可以看到標注了NotMapped的Sex屬性沒有映射到數據表的列中。
來看看沒有get和set的屬性的情況:修改Student實體
public class Student { private string _myschool; private string _myHobby; [Key] public int Key { get; set; } public string Name { get; set; } [NotMapped] public string Sex { get; set; } public int Age { get; set; } public string MySchool { get { return _myschool; } } public string MyHobby { set { _myHobby = value; } } }
運行程序:

看看數據庫:可以看到MySchool和MyHobby沒有映射為數據列。

再看看private屬性的情況,添加一個private屬性Address
public class Student { private string _myschool; private string _myHobby; [Key] public int Key { get; set; } public string Name { get; set; } [NotMapped] public string Sex { get; set; } public int Age { get; set; } public string MySchool { get { return _myschool; } } public string MyHobby { set { _myHobby = value; } } private string Address { get; set; } }
運行程序:

看看數據庫:可以看到Private屬性也不能映射到數據列。

總結:
1. NotMapped特性標識的屬性列,不會映射到數據庫
2.沒有get;set;的屬性不能映射到數據庫;
3.private屬性也不能映射到數據庫;
好了,大家有什么不明白的可以留言,我會一一回復,謝謝大家支持!
