在這一篇中,咱們將演示EnitityFramework基本的建模【建模也是EntityFramework最核心的特性】範例,例如實體的分離和繼承等。咱們開始了演示如何建立一個簡單的概念模型的例子,而後讓EnitityFramework創建底層數據庫。在餘下的例子中,咱們將告訴你如何從現有的表和數據庫關係建立模型。數據庫
1.點擊添加新建項,選擇Data下的ADO.NET實體模型,並選擇空模型。瀏覽器
2.右鍵選擇新增實體架構
3.將實體命名爲Person,實體集命名爲People,添加名爲Id,類型爲Int32的鍵屬性。學習
4.添加標量屬性this
5.添加標量屬性FirstName. LastName, MiddleName,PhoneNumber,同時指定鍵屬性Id的數據庫生成策略spa
6.空白處右鍵-->屬性,修改實體容器名稱爲EF6RecipesContext,數據庫架構名稱爲article2,這些都是爲更好地管理Model作的有意義的動做。3d
7.右鍵選擇根據模型生成數據庫,選擇建立新的鏈接,選擇目標數據庫code
8.在.edmx文件中生成倉儲模型,並運行數據庫腳本blog
using System; namespace EntityFrameworkDemo { class Program { static void Main() { using (var context=new EF6RecipesContext()) { var person = new Person { FirstName = "Robert", MiddleName = "Allen", LastName = "Doe", PhoneNumber = "867-5309" }; context.People.Add(person); person = new Person { FirstName = "John", MiddleName = "K.", LastName = "Smith", PhoneNumber = "824-3031" }; context.People.Add(person); person = new Person { FirstName = "Billy", MiddleName = "Albert", LastName = "Minor", PhoneNumber = "907-2212" }; context.People.Add(person); person = new Person { FirstName = "Kathy", MiddleName = "Anne", LastName = "Ryan", PhoneNumber = "722-0038" }; context.People.Add(person); context.SaveChanges(); } using (var context=new EF6RecipesContext()) { foreach (var person in context.People) { Console.WriteLine("{0} {1} {2}, Phone: {3}", person.FirstName, person.MiddleName, person.LastName, person.PhoneNumber); } } Console.ReadKey(); } } }
運行效果
繼承
至於使用using的好處,這裏就很少說了,由於這也是最基礎的。下面是書中的解釋片斷,不熟悉的同窗能夠看看
There are a few nice features of using()statements. First, when the code execution leaves the using() {}block,
the Dispose()method on the context will be called because DbContext implements the IDisposable interface. For
DbContext, the Dispose()method closes any active database connections and properly cleans up any other resources
that need to be released.
Second, no matter how the code leaves the using(){}block, the Dispose()method is called. Most importantly,
this includes return statements and exceptions that may be thrown within the code block. The using(){}block is kind
of a guarantee that critical resources will be reclaimed properly.
The best practice here is always to wrap your code in the using(){}block when creating new instances of
DbContext. It’s one more step to help bulletproof your code
問題
現有一個已存在的數據庫中的表,假設也有一些視圖,已經一些外鍵約束,你想爲這個數據庫建立模型
解決方案
你的數據庫中的結構多是這樣:
首先咱們安裝前面的步驟,打開向導,選擇由數據庫生成,並選擇須要操做的表和視圖
點擊完成後,EntityFramework會推斷出Poet和Poem,以及Meter和Poem之間一對多的關係
從模型瀏覽器中咱們能夠看出一首詩對應一個做者和一個分類,分別對應Poet和Meter導航屬性,若是咱們有一個Poem的實體,那麼導航屬性Poet也會包含一個詩人的實體的集合【由於是一對多的關係】,同理Meter也是如此。由於SQLSERVER不支持在視圖上定義關係,所以vwLiberary上市一組空的導航屬性
using (var context = new EF6RecipesEntities()) { var poet = new Poet {FirstName = "John", LastName = "Milton"}; var poem = new Poem {Title = "Paradise Lost"}; var meter = new Meter {MeterName = "Iambic Pentameter"}; poem.Meter = meter; poem.Poet = poet; context.Poems.Add(poem); poem = new Poem {Title = "Paradise Regained", Meter = meter, Poet = poet}; context.Poems.Add(poem); poet = new Poet {FirstName = "Lewis", LastName = "Carroll"}; poem = new Poem {Title = "The Hunting of the Shark"}; meter = new Meter {MeterName = "Anapestic Tetrameter"}; poem.Meter = meter; poem.Poet = poet; context.Poems.Add(poem); poet = new Poet {FirstName = "Lord", LastName = "Byron"}; poem = new Poem {Title = "Don Juan", Meter = meter, Poet = poet}; context.Poems.Add(poem); context.SaveChanges(); } using (var context = new EF6RecipesEntities()) { var poets = context.Poets; foreach (var poet in poets) { Console.WriteLine("{0} {1}", poet.FirstName, poet.LastName); foreach (var poem in poet.Poems) { Console.WriteLine("\t{0} ({1})", poem.Title, poem.Meter.MeterName); } } } // using our vwLibrary view using (var context = new EF6RecipesEntities()) { var items = context.vwLibraries; foreach (var item in items) { Console.WriteLine("{0} {1}", item.FirstName, item.LastName); Console.WriteLine("\t{0} ({1})", item.Title, item.MeterName); } }
運行效果
咱們使用SQLSERVER profile監視這段代碼的執行狀況發現,並非執行完var poets = context.vwLibraries;就當即去數據庫中抓取數據,而知在執行foreach的時候纔去查詢以後將結果存放在內存中對數據進行不通過數據庫直接從中讀取,總之當前能夠認爲它是用到的時候纔去執行,詳細的流程待後續學習在進行總結。