使用简介EntityFramework6.0
?
序言
在這一篇中,我們將演示EnitityFramework基本的建模【建模也是EntityFramework最核心的特性】范例,例如實體的分離和繼承等。我們開始了演示如何創(chuàng)建一個簡單的概念模型的例子,然后讓EnitityFramework建立底層數(shù)據(jù)庫。在余下的例子中,我們將告訴你如何從現(xiàn)有的表和數(shù)據(jù)庫關系創(chuàng)建模型。
創(chuàng)建一個簡單的Model
1.點擊添加新建項,選擇Data下的ADO.NET實體模型,并選擇空模型。
?2.右鍵選擇新增實體
3.將實體命名為Person,實體集命名為People,添加名為Id,類型為Int32的鍵屬性。
4.添加標量屬性
?
?5.添加標量屬性FirstName. LastName, MiddleName,PhoneNumber,同時指定鍵屬性Id的數(shù)據(jù)庫生成策略
?6.空白處右鍵-->屬性,修改實體容器名稱為EF6RecipesContext,數(shù)據(jù)庫架構名稱為article2,這些都是為更好地管理Model做的有意義的動作。
7.右鍵選擇根據(jù)模型生成數(shù)據(jù)庫,選擇創(chuàng)建新的連接,選擇目標數(shù)據(jù)庫
8.在.edmx文件中生成倉儲模型,并運行數(shù)據(jù)庫腳本
?它是怎么工作的
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
?從已存在的數(shù)據(jù)庫中生成模型
問題
現(xiàn)有一個已存在的數(shù)據(jù)庫中的表,假設也有一些視圖,已經(jīng)一些外鍵約束,你想為這個數(shù)據(jù)庫創(chuàng)建模型
解決方案
你的數(shù)據(jù)庫中的結構可能是這樣:
首先我們安裝前面的步驟,打開向導,選擇由數(shù)據(jù)庫生成,并選擇需要操作的表和視圖
點擊完成后,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 viewusing (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監(jiān)視這段代碼的執(zhí)行情況發(fā)現(xiàn),并不是執(zhí)行完var poets = context.vwLibraries;就立即去數(shù)據(jù)庫中抓取數(shù)據(jù),而知在執(zhí)行foreach的時候才去查詢之后將結果存放在內(nèi)存中對數(shù)據(jù)進行不經(jīng)過數(shù)據(jù)庫直接從中讀取,總之當前可以認為它是用到的時候才去執(zhí)行,詳細的流程待后續(xù)學習在進行總結。
?
轉載于:https://www.cnblogs.com/rohelm/p/3961767.html
總結
以上是生活随笔為你收集整理的使用简介EntityFramework6.0的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 视图之二--视图中数据的更新
- 下一篇: Qt调用dll中的功能函数