偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

LINQ to SQL刪除實(shí)現(xiàn)淺析

開發(fā) 后端
關(guān)于LINQ to SQL刪除的學(xué)習(xí)是我們掌握Linq to SQL操作的重要部分,那么我們?cè)诹私釲INQ to SQL查詢添加更新之后那么就要認(rèn)識(shí)LINQ to SQL刪除的執(zhí)行了,這里向你介紹具體的內(nèi)容。

我們?cè)趯W(xué)習(xí)了LINQ to SQL之查詢以及添加和更新的實(shí)現(xiàn)之后,現(xiàn)在我們來(lái)看看,LINQ to SQL是如何怎樣進(jìn)行刪除數(shù)據(jù)的,具體的實(shí)現(xiàn)過(guò)程和步驟是什么呢?讓我們來(lái)看看。

LINQ to SQL刪除數(shù)據(jù)以Northwind為例子:

1、首先以Customers表的一行數(shù)據(jù)為例,來(lái)實(shí)現(xiàn)LINQ to SQL刪除:

  1. NorthwindDataContext ctx = new NorthwindDataContext();  
  2.  
  3. Customer test1 = ctx.Customers.Single(c => c.CustomerID == "TEST1");  
  4.  
  5. ctx.Customers.Remove(test1);  
  6.  
  7. ctx.SubmitChanges();  

2、通過(guò)查看數(shù)據(jù)庫(kù)中的Customers表,可以發(fā)現(xiàn)該條數(shù)據(jù)已經(jīng)被刪除了。

  1. NorthwindDataContext ctx = new NorthwindDataContext();  
  2. Customer test1 = ctx.Customers.Single(c => c.CustomerID == "TEST1");  
  3. ctx.Customers.Remove(test1);  
  4. ctx.SubmitChanges();  
  5.  
  6. test1 = ctx.Customers.Single(c => c.CustomerID == "TEST1");  
  7. Console.WriteLine(test1.CustomerID);  

先刪除CustomerID為"TEST1"的一行數(shù)據(jù),然后再在數(shù)據(jù)庫(kù)中查詢?cè)摋l數(shù)據(jù),理論上來(lái)說(shuō)數(shù)據(jù)庫(kù)中該數(shù)據(jù)已經(jīng)不存在了,查詢出來(lái)應(yīng)該沒有結(jié)果??墒瞧聊惠敵鰹?TEST1",這是已經(jīng)被刪除的Customer的CustomerID。是不是會(huì)讓人覺得奇怪,數(shù)據(jù)庫(kù)中數(shù)據(jù)已經(jīng)不存在了,但是查詢還是可以得到正確的結(jié)果。其實(shí)原因也很簡(jiǎn)單,雖然在數(shù)據(jù)庫(kù)中該數(shù)據(jù)已經(jīng)被刪除,但是在DataContext中的Identity Cache還保存著對(duì)該對(duì)象的引用(什么是Identity Cache,前文已經(jīng)解釋過(guò)了),查詢出來(lái)的結(jié)果是在DataContext中Cache著的對(duì)象而不是存在于數(shù)據(jù)庫(kù)中的。可以知道如果在另一個(gè)DataContext中查詢?cè)摂?shù)據(jù),肯定是查詢不到的。

3、LINQ to SQL刪除中的級(jí)聯(lián)刪除,以Customers和Orders為例:

  1. NorthwindDataContext ctx = new NorthwindDataContext();  
  2. Customer test1 = ctx.Customers.Single(c => c.CustomerID == "TEST1");  
  3.  
  4. Order order1 = test1.Orders.Single(o => o.ShipCity == "Shanghai");  
  5. test1.Orders.Remove(order1);  
  6.  
  7. ctx.SubmitChanges();  

在該示例中,欲刪除CustomerID為"TEST1"的Customer的訂單中ShipCity為上海的訂單。執(zhí)行這段代碼,通過(guò)SQL Profile可以發(fā)現(xiàn),并沒有運(yùn)行delete from Orders...的SQL語(yǔ)句而是update,只是把Orders表中那條記錄的CustomerID設(shè)置為NULL,刪除的是該記錄與Customer的關(guān)系而并沒有真正刪除這條記錄。要想真正刪除該記錄必須通過(guò)DataContext來(lái)操作:

  1. ctx.Orders.Remove(order1);  
  2.  
  3. ctx.SubmitChanges(); 

這是在刪除過(guò)程中值得注意的一個(gè)問題。

要?jiǎng)h除Customer以及相關(guān)的Order應(yīng)該這樣來(lái)操作(也可以在數(shù)據(jù)庫(kù)中設(shè)置級(jí)聯(lián)刪除):

  1. NorthwindDataContext ctx = new NorthwindDataContext();  
  2. Customer test1 = ctx.Customers.Single(c => c.CustomerID == "TEST1");  
  3.  
  4. foreach (Order o in test1.Orders)  
  5. {  
  6. test1.Orders.Remove(o);  
  7. ctx.Orders.Remove(o);  
  8. }  
  9. ctx.Customers.Remove(test1);  
  10.  
  11. ctx.SubmitChanges();  

在數(shù)據(jù)刪除時(shí)也會(huì)遇到像數(shù)據(jù)更新一樣的沖突問題,解決方法基本相同這里就不多說(shuō)了。

原文來(lái)自博客園:http://www.cnblogs.com/blusehuang/archive/2007/07/08/810485.html

關(guān)于LINQ to SQL刪除實(shí)現(xiàn)問題就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)實(shí)現(xiàn)LINQ to SQL刪除有所幫助。

【編輯推薦】

  1. LINQ to SQL Table淺談
  2. Linq語(yǔ)句問題的解決方法
  3. Ling to sql更新實(shí)體概述
  4. Linq實(shí)體繼承簡(jiǎn)單描述
  5. Linq Library概述
責(zé)任編輯:仲衡 來(lái)源: 博客園
相關(guān)推薦

2009-09-16 17:11:35

LINQ To SQL

2009-09-10 18:02:23

LINQ to SQL

2009-09-14 10:12:11

LINQ to SQL

2009-09-14 13:17:51

LINQ to SQLLINQ to SQL

2009-09-15 10:12:37

LINQ To SQL

2009-09-17 18:05:15

linq to sql

2009-09-17 17:34:23

linq to sql

2009-09-14 18:23:59

LINQ嵌套查詢

2009-09-14 19:20:22

LINQ TO SQL

2009-09-10 10:09:46

LINQ to SQL

2009-09-14 19:14:51

LINQ動(dòng)態(tài)查詢

2009-09-18 14:25:36

LINQ to SQL

2009-09-14 17:40:47

LINQ To SQL

2009-09-14 16:29:39

LINQ嵌套

2009-09-17 08:47:00

Linq插入數(shù)據(jù)

2009-09-15 14:30:11

Linq連接

2010-09-01 15:30:24

SQL刪除

2009-09-15 13:30:54

linq級(jí)聯(lián)

2009-09-07 16:44:28

Linq String

2009-09-16 15:33:22

LINQ to XML
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)