ADO.NET Entity Framework,即下一代的ADO.NET。它是比Linq To SQL更加強大的ORM,讓開發人員只需要著眼於領域物件模型的開發,而不需要考慮它們是如何與關聯式資料庫互動。上一篇文章簡單介紹了在項目中如何使用ADO.NETEntity Framework,從現在開始,正式進入了ADO.NET的學習之旅。這篇文章主要介紹在ADO.NETEntity Framework中如何進行查詢(以Northwind資料庫為例)。
1. 使用EntityCommand進行查詢
在Entity Framework中,我們可以通過EntityCommand進行查詢,它的使用方法就像ADO.NET中的SqlCommand。不同的是SqlCommand使用標準SQL語句對資料庫進行查詢,而EntityCommand使用Entity SQL對EntityContainer進行查詢,當然最終Entity Framework會將Entity SQL轉換成標準SQL語句查詢資料庫。
EntityConnection con = new EntityConnection("Name=NorthwindEntities"); con.Open(); using (EntityCommand cmd = new EntityCommand("select value c from NorthwindEntities.Customers as c", con)) { EntityDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess); while (reader.Read()) { Console.WriteLine("ID [{0}], ContactTitle [{1}]", reader["CustomerID"], reader["ContactTitle"]); } }
首先是建立一個EntityConnection,它接受一個參數,表明使用的是在config檔案中的哪個連接字串。
<connectionStrings>
<add name="NorthwindEntities" connectionString="metadata=.\Northwind.csdl|.\Northwind.ssdl|.\Northwind.msl;provider=System.Data.SqlClient;provider connection string="Data Source=localhost\sqlexpress;Initial Catalog=Northwind;Integrated Security=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
可以看到這個連接字串和以往ADO.NET中使用的連接字串並不一樣。metadata:指明.csdl/.ssdl/.msl三個檔案的路徑,這三個檔案的作用以後再做說明。provider:表示使用的是SqlClient或者Oledb或者Odbc。provider connection string:這一項便是以往所用的連接字串。providerName表示現在用的是EntityClient。
接著構造EntityCommand,最後通過EntityDataReader進行資料的讀取,值得注意的是這裡的EntityCommand接受的是Entity SQL語句,而不是標準SQL語句,具體的Entity SQL文法可以參考說明文檔。
2. 使用ObjectQuery進行查詢
Entity Framework提供一個名為ObjectQuery的類,它讓開發人員通過Entity SQL進行查詢,查詢結果以對象的形式供使用。
using (NorthwindEntities ctx = new NorthwindEntities()) { ObjectQuery<Customer> query = ctx.CreateQuery<Customer>("NorthwindEntities.Customers"); ObjectResult<Customer> result = query.Execute(MergeOption.NoTracking); foreach (Customer c in result) { Console.WriteLine("ID [{0}], ContactTitle [{1}]", c.CustomerID, c.ContactTitle); } }
首先調用CreateQuery方法來建立ObjectQuery對象(當然這裡也可以使用new,只是傳進的參數不同而已),它接受Entity SQL語句作為參數。然後調用Execute方法進行查詢,它接受MergeOption枚舉型的參數,表示解決衝突的方法。查詢結果以對象的形式(這裡是Customer)儲存在ObjectResult中。
下面是使用new的寫法:
using (NorthwindEntities ctx = new NorthwindEntities()) { ObjectQuery<Customer> query = new ObjectQuery<Customer>("Customers", ctx); foreach (Customer c in query) { Console.WriteLine("ID [{0}], ContactTitle [{1}]", c.CustomerID, c.ContactTitle); } }
3. ADO.NET Entity Framework Tool自動產生Entities和各個對象的代碼,協助開發人員減少了很多體力活。這樣,我們可以簡單寫成:
using (NorthwindEntities ctx = new NorthwindEntities()) { foreach (Customer c in ctx.Customers) { Console.WriteLine("ID [{0}], ContactTitle [{1}]", c.CustomerID, c.ContactTitle); } }
其實這裡,也是使用ObjectQuery來進行查詢。當然,可以給查詢加上更多的條件,在上一篇文章中也有說明這裡就不寫了。
值得關注的是:自動產生的實體類中有這樣一個方法,比如Customer:
public static Customer CreateCustomer(string customerID, string companyName) { Customer customer = new Customer(); customer.CustomerID = customerID; customer.CompanyName = companyName; return customer; }
並不像以前一樣,提供帶參數的建構函式,而是提供CreateCustomer的靜態方法來構造Customer執行個體。這似乎和前一段時候貧血充血的問題有關了,實體物件該不該有行為,是貧血還是充血?雖然只是一個方法,不過相信這也表明了微軟的態度吧。