ADO.NET Entity Framework 是微軟以 ADO.NET 為基礎(chǔ)所發(fā)展出來的對象關(guān)系對應(yīng) (O/R Mapping) 解決方案,早期被稱為 ObjectSpace,現(xiàn)已經(jīng)包含在 Visual Studio 2008 Service Pack 1 以及 .NET Framework 3.5 Service Pack 1 中發(fā)表。
ADO.NET Entity Framework 以 Entity Data Model (EDM) 為主,將數(shù)據(jù)邏輯層切分為三塊,分別為 Conceptual Schema, Mapping Schema 與 Storage Schema 三層,其上還有 Entity Client,Object Context 以及 LINQ 可以使用。
[
編輯本段
]
背景
長久以來,程序設(shè)計師和數(shù)據(jù)庫總是保持著一種微妙的關(guān)系,在商用應(yīng)用程序中,數(shù)據(jù)庫一定是不可或缺的元件,這讓程序設(shè)計師一定要為了連接與訪問數(shù)據(jù)庫而去學習 SQL 指令,因此在信息業(yè)中有很多人都在研究如何將程序設(shè)計模型和數(shù)據(jù)庫集成在一起,對象關(guān)系對應(yīng) (Object-Relational Mapping) 的技術(shù)就是由此而生,像 Hibernate 或 NHibernate 都是這個技術(shù)下的產(chǎn)物,而微軟雖然有了 ADO.NET 這 個數(shù)據(jù)訪問的利器,但卻沒有像NHibernate這樣的對象對應(yīng)工具,因此微軟在.NET Framework 2.0發(fā)展時期,就提出了一個ObjectSpace的概念,ObjectSpace可以讓應(yīng)用程序可以用完全對象化的方法連接與訪問數(shù)據(jù)庫,其技術(shù)概念 與NHibernate相當類似,然而ObjectSpace工程相當大,在.NET Framework 2.0完成時仍無法全部完成,因此微軟將ObjectSpace納入下一版本的.NET Framework中,并且再加上一個設(shè)計的工具(Designer),構(gòu)成了現(xiàn)在的 ADO.NET Entity Framework。
Entity Framework 利用了抽象化數(shù)據(jù)結(jié)構(gòu)的方式,將每個數(shù)據(jù)庫對象都轉(zhuǎn)換成應(yīng)用程序?qū)ο?(entity),而數(shù)據(jù)字段都轉(zhuǎn)換為屬性 (property),關(guān)系則轉(zhuǎn)換為結(jié)合屬性 (association),讓數(shù)據(jù)庫的 E/R 模型完全的轉(zhuǎn)成對象模型,如此讓程序設(shè)計師能用最熟悉的編程語言來調(diào)用訪問。而在抽象化的結(jié)構(gòu)之下,則是高度集成與對應(yīng)結(jié)構(gòu)的概念層、對應(yīng)層和儲存層,以 及支持 Entity Framework 的數(shù)據(jù)提供者 (provider),讓數(shù)據(jù)訪問的工作得以順利與完整的進行。
(1) 概念層:負責向上的對象與屬性顯露與訪問。
(2) 對應(yīng)層:將上方的概念層和底下的儲存層的數(shù)據(jù)結(jié)構(gòu)對應(yīng)在一起。
(3) 儲存層:依不同數(shù)據(jù)庫與數(shù)據(jù)結(jié)構(gòu),而顯露出實體的數(shù)據(jù)結(jié)構(gòu)體,和 Provider 一起,負責實際對數(shù)據(jù)庫的訪問和 SQL 的產(chǎn)生。
[
編輯本段
]
架構(gòu)
概念層結(jié)構(gòu)
概念層結(jié)構(gòu)定義了對象模型 (Object Model),讓上層的應(yīng)用程序碼可以如面向?qū)ο蟮姆绞桨阍L問數(shù)據(jù),概念層結(jié)構(gòu)是由 CSDL (Conceptual Schema Definition Language) 所撰寫1。
一份概念層結(jié)構(gòu)定義如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Schema Namespace="Employees" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
<EntityContainer Name="EmployeesContext">
<EntitySet Name="Employees" EntityType="Employees.Employees" />
</EntityContainer>
<EntityType Name="Employees">
<Key>
<PropertyRef Name="EmployeeId" />
</Key>
<Property Name="EmployeeId" Type="Guid" Nullable="false" />
<Property Name="LastName" Type="String" Nullable="false" />
<Property Name="FirstName" Type="String" Nullable="false" />
<Property Name="Email" Type="String" Nullable="false" />
</EntityType>
</Schema>
對應(yīng)層結(jié)構(gòu)
對應(yīng)層結(jié)構(gòu)負責將上層的概念層結(jié)構(gòu)以及下層的儲存體結(jié)構(gòu)中的成員結(jié)合在一起,以確認數(shù)據(jù)的來源與流向。對應(yīng)層結(jié)構(gòu)是由 MSL (Mapping Specification Language) 所撰寫2。
一份對應(yīng)層結(jié)構(gòu)定義如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Mapping Space="C-S" xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS">
<EntityContainerMapping StorageEntityContainer="dbo" CdmEntityContainer="EmployeesContext">
<EntitySetMapping Name="Employees" StoreEntitySet="Employees" TypeName="Employees.Employees">
<ScalarProperty Name="EmployeeId" ColumnName="EmployeeId" />
<ScalarProperty Name="LastName" ColumnName="LastName" />
<ScalarProperty Name="FirstName" ColumnName="FirstName" />
<ScalarProperty Name="Email" ColumnName="Email" />
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
儲存層結(jié)構(gòu)
儲存層結(jié)構(gòu)是負責與 數(shù)據(jù)庫管理系統(tǒng) (DBMS) 中的數(shù)據(jù)表做實體對應(yīng) (Physical Mapping),讓數(shù)據(jù)可以輸入正確的數(shù)據(jù)來源中,或者由正確的數(shù)據(jù)來源取出。它是由 SSDL (Storage Schema Definition Language) 所撰寫3。
一份儲存層結(jié)構(gòu)定義如下所示:
?xml version="1.0" encoding="utf-8"?>
<Schema Namespace="Employees.Store" Alias="Self"
Provider="System.Data.SqlClient"
ProviderManifestToken="2005"
xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
<EntityContainer Name="dbo">
<EntitySet Name="Employees" EntityType="Employees.Store.Employees" />
</EntityContainer>
<EntityType Name="Employees">
<Key>
<PropertyRef Name="EmployeeId" />
</Key>
<Property Name="EmployeeId" Type="uniqueidentifier" Nullable="false" />
<Property Name="LastName" Type="nvarchar" Nullable="false" MaxLength="50" />
<Property Name="FirstName" Type="nvarchar" Nullable="false" />
<Property Name="Email" Type="nvarchar" Nullable="false" />
</EntityType>
</Schema>
[
編輯本段
]
用戶端支持
當定義好 Entity Data Model 的 CS/MS/SS 之后,即可以利用 ADO.NET Entity Framework 的用戶端來訪問 EDM,EDM 中的數(shù)據(jù)提供者會向數(shù)據(jù)來源訪問數(shù)據(jù),再傳回用戶端。
目前 ADO.NET Entity Framework 有三種用戶端4:
Entity Client
Entity Client 是 ADO.NET Entity Framework 中的本地用戶端 (Native Client),它的對象模型和 ADO.NET 的其他用戶端非常相似,一樣有 Connection, Command, DataReader 等對象,但最大的差異就是,它有自己的 SQL 指令 (Entity SQL),可以用 SQL 的方式訪問 EDM,簡單的說,就是把 EDM 當成一個實體數(shù)據(jù)庫。
// Initialize the EntityConnectionStringBuilder.
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
//Set the provider name.
entityBuilder.Provider = providerName;
// Set the provider-specific connection string.
entityBuilder.ProviderConnectionString = providerString;
// Set the Metadata location.
entityBuilder.Metadata = @"res://*/AdventureWorksModel.csdl|
res://*/AdventureWorksModel.ssdl|
res://*/AdventureWorksModel.msl";
Console.WriteLine(entityBuilder.ToString());
using (EntityConnection conn = new EntityConnection(entityBuilder.ToString()))
{
conn.Open();
Console.WriteLine("Just testing the connection.");
conn.Close();
}
Object Context
由于 Entity Client 太過于制式,而且也不太符合 ORM 的精神,因此微軟在 Entity Client 的上層加上了一個供編程語言直接訪問的界面,它可以把 EDM 當成對象般的訪問,此界面即為 Object Context (Object Service)。
在 Object Context 中對 EDM 的任何動作,都會被自動轉(zhuǎn)換成 Entity SQL 送到 EDM 中執(zhí)行。
// Get the contacts with the specified name.
ObjectQuery<Contact> contactQuery = context.Contact
.Where("it.LastName = @ln AND it.FirstName = @fn",
new ObjectParameter("ln", lastName),
new ObjectParameter("fn", firstName));
LINQ to Entities
Object Context 將 EDM 的訪問改變?yōu)橐环N對對象集合的訪問方式,這也就讓 LINQ 有了發(fā)揮的空間,因此 LINQ to Entities 也就由此而生,簡單的說,就是利用 LINQ 來訪問 EDM,讓 LINQ 的功能可以在數(shù)據(jù)庫中發(fā)揮。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
ObjectQuery<Product> products = AWEntities.Product;
IQueryable<Product> productNames =
from p in products
select p;
[
編輯本段
]
開發(fā)工具
目前 ADO.NET Entity Framework 的開發(fā),在 Visual Studio 2008 中有充份的支持,在安裝 Visual Studio 2008 Service Pack 1 后,文件范本中即會出現(xiàn) ADO.NET 實體數(shù)據(jù)模型 (ADO.NET Entity Data Model) 可讓開發(fā)人員利用 Entity Model Designer 來設(shè)計 EDM,EDM 亦可由記事本或文本編輯器所編輯。
[
編輯本段
]
派生服務(wù)
主條目:ADO.NET Data Services
微軟特別針對了網(wǎng)絡(luò)上各種不同的應(yīng)用程序 (例如 AJAX, Silverlight, Mashup 應(yīng)用程序) 開發(fā)了一個基于 ADO.NET Entity Framework 之上的服務(wù),稱為 ADO.NET Data Services (項目代號為 Astoria),并與 ADO.NET Entity Framework 一起包裝在 .NET Framework 3.5 Service Pack 1 中發(fā)表。
[
編輯本段
]
支持廠商
目前已有數(shù)個數(shù)據(jù)庫廠商或元件開發(fā)商宣布要支持 ADO.NET Entity Framework:
(1) Core Lab,支持Oracle、MySQL、PostgreSQL 與 SQLite 數(shù)據(jù)庫。
(2) IBM,實現(xiàn) DB2 使用的 LINQ Provider。
(3) MySQL,發(fā)展 MySQL Server 所用的 Provider。
(4) Npqsql,發(fā)展 PostgreSQL 所用的 Provider。
(5) OpenLink Software,發(fā)展支持多種數(shù)據(jù)庫所用的 Provider。
(6) Phoenix Software International,發(fā)展支持 SQLite 數(shù)據(jù)庫的 Provider。
(7) Sybase,將支持 Anywhere 數(shù)據(jù)庫。
(8) VistaDB Software,將支持 VistaDB 數(shù)據(jù)庫。
(9) DataDirect Technologies,發(fā)展支持多種數(shù)據(jù)庫所用的 Provider。
(10) Firebird,支持 Firebird 數(shù)據(jù)庫。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

