使用简介EntityFramework6.0
?
序言
在這一篇中,我們將演示EnitityFramework基本的建模【建模也是EntityFramework最核心的特性】范例,例如實(shí)體的分離和繼承等。我們開始了演示如何創(chuàng)建一個(gè)簡(jiǎn)單的概念模型的例子,然后讓EnitityFramework建立底層數(shù)據(jù)庫(kù)。在余下的例子中,我們將告訴你如何從現(xiàn)有的表和數(shù)據(jù)庫(kù)關(guān)系創(chuàng)建模型。
創(chuàng)建一個(gè)簡(jiǎn)單的Model
1.點(diǎn)擊添加新建項(xiàng),選擇Data下的ADO.NET實(shí)體模型,并選擇空模型。
?2.右鍵選擇新增實(shí)體
3.將實(shí)體命名為Person,實(shí)體集命名為People,添加名為Id,類型為Int32的鍵屬性。
4.添加標(biāo)量屬性
?
?5.添加標(biāo)量屬性FirstName. LastName, MiddleName,PhoneNumber,同時(shí)指定鍵屬性Id的數(shù)據(jù)庫(kù)生成策略
?6.空白處右鍵-->屬性,修改實(shí)體容器名稱為EF6RecipesContext,數(shù)據(jù)庫(kù)架構(gòu)名稱為article2,這些都是為更好地管理Model做的有意義的動(dòng)作。
7.右鍵選擇根據(jù)模型生成數(shù)據(jù)庫(kù),選擇創(chuàng)建新的連接,選擇目標(biāo)數(shù)據(jù)庫(kù)
8.在.edmx文件中生成倉(cāng)儲(chǔ)模型,并運(yùn)行數(shù)據(jù)庫(kù)腳本
?它是怎么工作的
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();}} }運(yùn)行效果
?至于使用using的好處,這里就不多說(shuō)了,因?yàn)檫@也是最基礎(chǔ)的。下面是書中的解釋片段,不熟悉的同學(xué)可以看看
????? 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ù)庫(kù)中生成模型
問(wèn)題
現(xiàn)有一個(gè)已存在的數(shù)據(jù)庫(kù)中的表,假設(shè)也有一些視圖,已經(jīng)一些外鍵約束,你想為這個(gè)數(shù)據(jù)庫(kù)創(chuàng)建模型
解決方案
你的數(shù)據(jù)庫(kù)中的結(jié)構(gòu)可能是這樣:
首先我們安裝前面的步驟,打開向?qū)?#xff0c;選擇由數(shù)據(jù)庫(kù)生成,并選擇需要操作的表和視圖
點(diǎn)擊完成后,EntityFramework會(huì)推斷出Poet和Poem,以及Meter和Poem之間一對(duì)多的關(guān)系
從模型瀏覽器中我們可以看出一首詩(shī)對(duì)應(yīng)一個(gè)作者和一個(gè)分類,分別對(duì)應(yīng)Poet和Meter導(dǎo)航屬性,如果我們有一個(gè)Poem的實(shí)體,那么導(dǎo)航屬性Poet也會(huì)包含一個(gè)詩(shī)人的實(shí)體的集合【因?yàn)槭且粚?duì)多的關(guān)系】,同理Meter也是如此。因?yàn)镾QLSERVER不支持在視圖上定義關(guān)系,因此vwLiberary上市一組空的導(dǎo)航屬性
它是怎么工作的
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);}}?
運(yùn)行效果
我們使用SQLSERVER?profile監(jiān)視這段代碼的執(zhí)行情況發(fā)現(xiàn),并不是執(zhí)行完var poets = context.vwLibraries;就立即去數(shù)據(jù)庫(kù)中抓取數(shù)據(jù),而知在執(zhí)行foreach的時(shí)候才去查詢之后將結(jié)果存放在內(nèi)存中對(duì)數(shù)據(jù)進(jìn)行不經(jīng)過(guò)數(shù)據(jù)庫(kù)直接從中讀取,總之當(dāng)前可以認(rèn)為它是用到的時(shí)候才去執(zhí)行,詳細(xì)的流程待后續(xù)學(xué)習(xí)在進(jìn)行總結(jié)。
?
轉(zhuǎn)載于:https://www.cnblogs.com/rohelm/p/3961767.html
總結(jié)
以上是生活随笔為你收集整理的使用简介EntityFramework6.0的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 视图之二--视图中数据的更新
- 下一篇: 人生无悔的句子229个