現在假想,我們想要為謳歌學校創建一個應用程序,這個程序需要能夠來添加或者更新學生,分數,教師還有課程信息。
代替之前我們的做法:先是創建數據庫,現在我們不這么做,我們先來創建領域類,首先我來創建兩個簡單的類,一個是Student類,一個是Standard類。
每個學生都有一個分數,下面看代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF1 { public class Student { public int StudentId { get; set; } public string StudentName { get; set; } public DateTime DateOfBirth { get; set; } public byte[] Photo { get; set; } public decimal Height { get; set; } public float Weight { get; set; } public Standard Standard { get; set; } } }
這個Standard(分數類)需要容納下復數個學生(Student )
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace EF1 { public class Standard { public int StandardId { get; set; } public string StandardName { get; set; } public ICollection<Student> Students { get; set; } } }
現在,我們已經為我們的程序完成了,領域類初始化的工作。Code-First方法,同樣需要一個繼承自DbContext的上下文類。了解更多上下文類的信息請點擊: DbContext
創建上下文類的代碼如下,我們這個上下文類,繼承自DbContext類,然后暴露你想要的模型的實體的DbSet屬性,例如,Student和Standard類,在這個例子中。DbSet是實體的集合(also konw as entity set),所以我們給這個實體的屬性名字是復數的。
using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF1 { public class DbContextClass:DbContext { public DbContextClass() : base() { } public DbSet<Student> Studnets { get; set; } public DbSet<Standard> Standards { get; set; } } }
現在我們已經創建了必須的領域類和上下文類,現在讓我們使用上下文,來添加一個學生信息吧
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF1 { class Program { static void Main(string[] args) { Student stu = new Student() { StudentId = 1, StudentName = "韋小寶", Height = 172, Weight = 120 ,DateOfBirth=DateTime.Now}; using (var db = new DbContextClass()) { db.Studnets.Add(stu); db.SaveChanges(); } Console.WriteLine("添加成功"); Console.ReadKey(); } } }
運行的效果圖:
運行成功了?那么你會奇怪,數據庫呢,數據表呢,別奇怪,慢慢看:
This is the beauty of Code-First APIs of Entity Framework. It creates the database based on parameter passed in the base constructor of your context class. Since we have not passed any parameter in the constructor of our context class, it created "EF_Code_First_Tutorials.SchoolContext" database in the local SQLEXPRESS database, as shown below. It also created two tables in this database, Students and Standards tables based on Student and Standard domain classes defined above.
這就是Code-First APIs的優點,它基于上下文類中的base構造器中的參數,為我們創建了數據庫。盡管,我們沒有傳遞任何參數到構造器中,它為我們在這個目錄下面【C:\Users\XXXXXX】創建了這個數據庫:
注意:因為我的數據庫版本不是免費版的,所以創建的數據庫,在這個目錄下面:【C:\Users\XXXXXX】
現在我想把這個數據庫,附加到我的SQL中,查看里面的表,但是出錯了:
版本不支持,我數據庫的版本是SQL2008,這個數據庫的版本,應該是比2008低級的版本。
在網上百度了一下:解決方案地址:http://www.2cto.com/database/201404/294024.html
“數據庫 的版本為 661,無法打開。此服務器支持 655 版及更低版本。不支持降級路徑”
出現這樣的問題,一般是因為數據庫版本不同造成的。
我們可以用下面的語句查詢數據庫的版本
use master
select @@VERSION
(1)661是sql2008 R2的版本號
Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (Intel X86) Apr 2 2010 15:53:02 Copyright (c) Microsoft
(2)655版本也就是sql2008 sp1版本號
Microsoft SQL Server 2008 (SP1) - 10.0.2531.0 (Intel X86) Mar 29 2009 10:27:29 Copyright (c) 1988-2008
如果后面是1600,則表明只安裝了sql2008r2,還要裝一個sql2008sp1。這樣才能保證后面是2500.
剛好我的數據庫,是第一個版本。我來找一個SQL2008Sp1。
這個還是不行,需要安裝SQL2012。。。。。。
自己也懶得安裝SQL2012,既然那這種辦法不行,那就想起他的辦法,現在我們來修改一下,上下文類:
using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF1 { public class DbContextClass:DbContext { public DbContextClass() : base("ConnectionString") { } public DbSet<Student> Studnets { get; set; } public DbSet<Standard> Standards { get; set; } } }
然后我們改一下配置文件加上這個節點:
<connectionStrings>
<add name="ConnectionString" connectionString="server=.;database=EFCodeFirstDB;uid=sa;pwd=Password_1" providerName="System.Data.SqlClient"/>
</connectionStrings>
運行程序,成功之后,我們打開數據庫。奇跡出現了!
生成了數據庫,在SQL2008中:
然后我們打開看里面的表,字段:
As you can see in the above figure, it has created Students and Standards tables and each table contains columns with appropriate datatype and length. The column names and datatype matches with the properties of the respective domain classes. It has also made StudentId and StandardId as PK (primary key) and Standard_StandardId column as FK (foreign key).
This way, without creating a database first, you can start writing an application that will eventually create the database from your domain classes.
You must be wondering how it has created columns with appropriate datatypes and lengh with PK & FK, right? The answer is, using code-first conventions.
Learn code-first conventions in the next section.
你可以看到上面的圖片中,創建了數據庫和表,并且還有主外鍵。這些都是Code-First約定幫助我們實現的。
附上系列目錄:
- 什么是Code First
- 簡單的Code First例子
- Code-First 約定
- DB Initialization(數據庫初始化)
- Inheritance Strategy(繼承策略)
- Configure Domain Classes(配置領域類)
- DataAnnotations(數據注解)
- Fluent API
- Configure One-to-One(配置一對一關系)
- Configure One-to-Many(配置一對多關系)
- Configure Many-to-Many(配置多對多關系)
- Move Configurations(數據遷移)
- DB Initialization Strategy(數據庫初始化策略)
- ...待續.....
文章列表