Linq to SQL之查詢和添加

來源:互聯網
上載者:User

 

   本文以Northwind資料庫中的Customers和Orders表為例說明Linq to SQL的查詢和添加是怎樣操作的。

首先通過Linq to SQL File嚮導建立DataContext以及相應的Entity Class,如果不知道怎樣做請看這裡:Defining data model classes

建立一個Customer對象,然後把該資料添加到資料庫中去。

  NorthwindDataContext ctx = new NorthwindDataContext();  Customer c = new Customer { CustomerID = "Test1",      CompanyName = "Test Company1" };  ctx.Customers.Add(c);  ctx.SubmitChanges();

運行這段代碼,在sql profile中可以看到

  exec sp_executesql N'INSERT INTO [dbo].[Customers]([CustomerID], [CompanyName], [ContactName]...

這條sql語句,說明資料插入到Customer表中。然後再運行下面這段代碼,看看能不能把剛才添加的資料從資料庫中查詢出來

  Customer c1 = ctx.Customers.Single(c => c.CustomerID == "Test1");  Console.WriteLine(c1.CustomerID + "  " + c1.CompanyName);

運行這段代碼後,在sql profile中可以看到執行了這條sql語句

  exec sp_executesql N'SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName]...

螢幕上也列印出正確的結果

 

現在讓下面的代碼一次執行看看會發生什嗎?

  NorthwindDataContext ctx = new NorthwindDataContext();  Customer c = new Customer  {      CustomerID = "Test2",      CompanyName = "Test Company2"  };  ctx.Customers.Add(c);  ctx.SubmitChanges();  Customer c1 = ctx.Customers.Single(ct => ct.CustomerID == "Test2");  Console.WriteLine(c1.CustomerID + "  " + c1.CompanyName);

通過sql profile中的記錄,可以發現這次只執行了插入語句而沒有執行查詢語句,但是查詢又的確返回了正確的結果。Mtauly在video裡說這是Linq to SQL的一種Cache機制,稱為Identity Cache。執行查詢的時候,如果datacontext發現查詢條件是主鍵而且只有主鍵的時候,它會先在Cache中尋找,如果找到了就返回該對象,可以猜想在datacontext內部,可能用一個hashtable類似的容易,以主鍵的值為key來快取作業過的對象。 而當我們用另一個datacontext進行查詢或者查詢條件不只包含主鍵的時候,它會進行一次對資料庫的查詢。

 

 下面來看看Linq to SQL怎樣進行一對多關聯性的查詢。其實很簡單:

  Customer c = ctx.Customers.Single(ct => ct.CompanyName == "Test Company1");  foreach (var order in c.Orders)  {      Console.WriteLine(order);  }

 通過觀察sql profile,發現只有使用c.Orders,才會對orders表進行查詢,而在查詢customer的時候不會把與之相關的Orders全部返回,這類似與Hibernate裡面的lazy initialization機制吧。 再者,Linq to SQL裡面提供了一種稱為Delay Loaded機制,在欄位上使用。意思是只有當使用這些欄位的時候,才進行一次資料庫查詢把這些欄位選取出來提供使用,對於包含大資料欄位(picture、blob)資料的表,該機制也能提高不少效率。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.