原文鏈接:http://www.entityframeworktutorial.net/code-first/simple-code-first-example.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系列】
在前面一節中,我們學會了怎么,在我們項目中安裝Entity Framework。現在我們來創建一個簡單的Code-First示例吧:
假定,我們想為XYZ學校創建一個簡單的應用程序,使用這個程序來添加、更新學生信息,以及學生分數信息,班級信息還有老師信息。
不像之前傳統做法那樣,先創建數據庫,這里我們為我們的學生創建學生領域類。首先創建Student,以及Grade實體,代碼中一個學生實體和一個Grade實體相關聯。這被稱作為一對多的關系。后面的章節中,我們將會學到EF怎么管理實體之間的關系。
我們創建一個控制台應用程序,安裝好EF。
然后創建Student 和Grade類
public class Student { /// <summary>
/// 學生ID /// </summary>
public int StudentID { get; set; } /// <summary>
/// 學生姓名 /// </summary>
public string StudentName { get; set; } /// <summary>
/// 出生日期 /// </summary>
public DateTime? DateOfBirth { get; set; } /// <summary>
/// 圖片 /// </summary>
public byte[] Photo { get; set; } /// <summary>
/// 身高 /// </summary>
public decimal Height { get; set; } /// <summary>
/// 體重 /// </summary>
public float Weight { get; set; } /// <summary>
/// 導航屬性--Grade--年級 /// </summary>
public Grade Grade { get; set; } }
public class Grade { public int GradeID { get; set; } public string GradeName { get; set; } public string Section { get; set; } public ICollection<Student> Students { get; set; } }
如上,我們創建好了程序的領域類。
Code-First方式,同樣還需要一個上下文類,上下文類是繼承自DbContext類的類。創建一個上下文類,如下,它繼承自DbContext,暴露兩個屬性Students和Grades.
現在,我們已經創建好了Code-First方式需要的類,現在來測試一下:
運行項目:
表示一個學生信息已經成功添加到了數據庫中。
但是,數據庫在哪里呢?數據表呢?數據列呢?
這就是EF Code-First API 迷人之處。它基於你在上下文類的構造函數中傳遞的參數,創建數據庫。因為這里,我們在上下文類的構造函數中沒有傳遞任何參數,EF 就給我們在這里創建了數據庫:
C:\Users\你的計算機名稱
這個數據庫不能附加,我們可以在配置文件,配置一下:
然后修改一下,上下文類的構造函數:
再運行項目看看:
說明成功創建了數據庫,我們看下生成的數據庫:
就像上面圖形中所示,EF為我們創建了數據庫,還創建了相應的數據表,數據列。這樣我們就使用Code-First,創建了數據庫.
請注意:如果你修改了實體,再運行項目的話,就會報錯:例如,我在Student實體中加一個屬性Email。
然后運行項目:
出現這個錯誤,是需要進行數據庫遷移。你需要在上下文類中,定義數據庫初始化策略,然后修改實體,最后運行項目。數據庫遷移技術,我好多系列文章中都講到了,當然 這個系列還是會告訴大家怎么配置,大家可以到時候好好跟着我鞏固一下。
你現在肯定很好奇,EF API是怎么創建合適類型的數據列的,怎么創建主鍵,怎么創建外鍵的?答案就是使用Code-First默認的約定配置。下面一節中,我們將學習Code-First默認約定。