當前位置:編程學習大全網 - 編程語言 - ADO.NET Entity Framework的用戶端

ADO.NET Entity Framework的用戶端

當定義好 Entity Data Model 的 CS/MS/SS 之後,即可以利用 ADO.NET Entity Framework 的用戶端來訪問 EDM,EDM 中的數據提供者會向數據來源訪問數據,再傳回用戶端。

目前 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 當成壹個實體數據庫。

// 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 的任何動作,都會被自動轉換成 Entity SQL 送到 EDM 中執行。

// 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 的訪問改變為壹種對對象集合的訪問方式,這也就讓 LINQ 有了發揮的空間,因此 LINQ to Entities 也就由此而生,簡單的說,就是利用 LINQ 來訪問 EDM,讓 LINQ 的功能可以在數據庫中發揮。

using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())

{

ObjectQuery<Product> products = AWEntities.Product;

IQueryable<Product> productNames =

from p in products

select p;

  • 上一篇:石墨電極的工藝流程詳解
  • 下一篇:jndi(java命名與目錄接口)api下有哪些常用的包和接口?
  • copyright 2024編程學習大全網