{"id":129,"date":"2008-05-01T17:41:49","date_gmt":"2008-05-01T17:41:49","guid":{"rendered":""},"modified":"2008-05-01T17:41:49","modified_gmt":"2008-05-01T17:41:49","slug":"129","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=129","title":{"rendered":"LINQ\u4f53\u9a8c(10)\u2014\u2014LINQ to SQL\u8bed\u53e5\u4e4b\u5f00\u653e\u5f0f\u5e76\u53d1\u63a7\u5236\u548c\u4e8b\u52a1"},"content":{"rendered":"<p>\n\u5fae\u8f6f\u4e8e2\u67081\u65e5\u53d1\u5e03\u4e86Visual Studio Team System 2008 Team Suite\u7b80\u4f53\u4e2d\u6587\u7248\uff0c\u60a8\u53ef\u4ee5\u5728<a target=\"_blank\" href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?familyid=D95598D7-AA6E-4F24-82E3-81570C5384CB&amp;displaylang=zh-cn\"><font color=\"#000000\">\u8fd9\u91cc<\/font><\/a>\u4e0b\u8f7dVisual Studio Team System 2008 Team Suite\u7b80\u4f53\u4e2d\u6587\u724890 \u5929\u8bd5\u7528\u7248\u3002\u4eca\u5929\u7b80\u5355\u7684\u5b66\u4e60\u4e0b\u5f00\u653e\u5f0f\u5e76\u53d1\u63a7\u5236\u548c\u4e8b\u52a1\u7684\u5185\u5bb9\uff0c\u5177\u4f53\u8be6\u7ec6\u7684\u5185\u5bb9\u73b0\u5728\u53ef\u4ee5\u53c2\u770bMSDN\u4e86\u3002<\/p>\n<h1 class=\"hcode\"><font size=\"4\">Simultaneous Changes\u5f00\u653e\u5f0f\u5e76\u53d1\u63a7\u5236<\/font><\/h1>\n<p>\u4e0b\u8868\u4ecb\u7ecd LINQ to SQL \u6587\u6863\u4e2d\u6d89\u53ca\u5f00\u653e\u5f0f\u5e76\u53d1\u7684\u672f\u8bed\uff1a<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<table cellspacing=\"0\" cellpadding=\"0\" border=\"1\">\n<tbody>\n<tr>\n<th>\u672f\u8bed<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<tr>\n<td>\u5e76\u53d1<\/td>\n<td>\u4e24\u4e2a\u6216\u66f4\u591a\u7528\u6237\u540c\u65f6\u5c1d\u8bd5\u66f4\u65b0\u540c\u4e00\u6570\u636e\u5e93\u884c\u7684\u60c5\u5f62\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u5e76\u53d1\u51b2\u7a81<\/td>\n<td>\u4e24\u4e2a\u6216\u66f4\u591a\u7528\u6237\u540c\u65f6\u5c1d\u8bd5\u5411\u4e00\u884c\u7684\u4e00\u5217\u6216\u591a\u5217\u63d0\u4ea4\u51b2\u7a81\u503c\u7684\u60c5\u5f62\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u5e76\u53d1\u63a7\u5236<\/td>\n<td>\u7528\u4e8e\u89e3\u51b3\u5e76\u53d1\u51b2\u7a81\u7684\u6280\u672f\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u5f00\u653e\u5f0f\u5e76\u53d1\u63a7\u5236<\/td>\n<td>\u5148\u8c03\u67e5\u5176\u4ed6\u4e8b\u52a1\u662f\u5426\u5df2\u66f4\u6539\u4e86\u884c\u4e2d\u7684\u503c\uff0c\u518d\u5141\u8bb8\u63d0\u4ea4\u66f4\u6539\u7684\u6280\u672f\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u4fdd\u5b88\u5f0f\u5e76\u53d1\u63a7\u5236\u5219\u662f\u901a\u8fc7\u9501\u5b9a\u8bb0\u5f55\u6765\u907f\u514d\u53d1\u751f\u5e76\u53d1\u51b2\u7a81\u3002\u4e4b\u6240\u4ee5\u79f0\u4f5c\u5f00\u653e\u5f0f\u63a7\u5236\uff0c\u662f\u56e0\u4e3a\u5b83\u5c06\u4e00\u4e2a\u4e8b\u52a1\u5e72\u6270\u53e6\u4e00\u4e8b\u52a1\u89c6\u4e3a\u4e0d\u592a\u53ef\u80fd\u53d1\u751f\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u51b2\u7a81\u89e3\u51b3<\/td>\n<td>\u901a\u8fc7\u91cd\u65b0\u67e5\u8be2\u6570\u636e\u5e93\u5237\u65b0\u51fa\u73b0\u51b2\u7a81\u7684\u9879\uff0c\u7136\u540e\u534f\u8c03\u5dee\u5f02\u7684\u8fc7\u7a0b\u3002\u5237\u65b0\u5bf9\u8c61\u65f6\uff0cLINQ to SQL \u66f4\u6539\u8ddf\u8e2a\u5668\u4f1a\u4fdd\u7559\u4ee5\u4e0b\u6570\u636e\uff1a<br \/>\n            \u6700\u521d\u4ece\u6570\u636e\u5e93\u83b7\u53d6\u5e76\u7528\u4e8e\u66f4\u65b0\u68c0\u67e5\u7684\u503c \u901a\u8fc7\u540e\u7eed\u67e5\u8be2\u83b7\u5f97\u7684\u65b0\u6570\u636e\u5e93\u503c\u3002 <br \/>\n            LINQ to SQL \u968f\u540e\u4f1a\u786e\u5b9a\u76f8\u5e94\u5bf9\u8c61\u662f\u5426\u53d1\u751f\u51b2\u7a81\uff08\u5373\u5b83\u7684\u4e00\u4e2a\u6216\u591a\u4e2a\u6210\u5458\u503c\u662f\u5426\u5df2\u53d1\u751f\u66f4\u6539\uff09\u3002\u5982\u679c\u6b64\u5bf9\u8c61\u53d1\u751f\u51b2\u7a81\uff0cLINQ to SQL \u4e0b\u4e00\u6b65\u4f1a\u786e\u5b9a\u5b83\u7684\u54ea\u4e9b\u6210\u5458\u53d1\u751f\u51b2\u7a81\u3002LINQ to SQL \u53d1\u73b0\u7684\u4efb\u4f55\u6210\u5458\u51b2\u7a81\u90fd\u4f1a\u6dfb\u52a0\u5230\u51b2\u7a81\u5217\u8868\u4e2d\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5728 LINQ to SQL \u5bf9\u8c61\u6a21\u578b\u4e2d\uff0c\u5f53\u4ee5\u4e0b\u4e24\u4e2a\u6761\u4ef6\u90fd\u5f97\u5230\u6ee1\u8db3\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f&ldquo;\u5f00\u653e\u5f0f\u5e76\u53d1\u51b2\u7a81&rdquo;\uff1a\u5ba2\u6237\u7aef\u5c1d\u8bd5\u5411\u6570\u636e\u5e93\u63d0\u4ea4\u66f4\u6539\uff1b\u6570\u636e\u5e93\u4e2d\u7684\u4e00\u4e2a\u6216\u591a\u4e2a\u66f4\u65b0\u68c0\u67e5\u503c\u81ea\u5ba2\u6237\u7aef\u4e0a\u6b21\u8bfb\u53d6\u5b83\u4eec\u4ee5\u6765\u5df2\u5f97\u5230\u66f4\u65b0\u3002 \u6b64\u51b2\u7a81\u7684\u89e3\u51b3\u8fc7\u7a0b\u5305\u62ec\u67e5\u660e\u5bf9\u8c61\u7684\u54ea\u4e9b\u6210\u5458\u53d1\u751f\u51b2\u7a81\uff0c\u7136\u540e\u51b3\u5b9a\u60a8\u5e0c\u671b\u5982\u4f55\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<h1 class=\"hcode\"><font size=\"4\">Optimistic Concurrency<\/font><\/h1>\n<p>\u8bf4\u660e\uff1a\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u5728\u4f60\u8bfb\u53d6\u6570\u636e\u4e4b\u524d\uff0c\u53e6\u5916\u4e00\u4e2a\u7528\u6237\u5df2\u7ecf\u4fee\u6539\u5e76\u63d0\u4ea4\u66f4\u65b0\u4e86\u8fd9\u4e2a\u6570\u636e\uff0c\u6240\u4ee5\u4e0d\u4f1a\u51fa\u73b0\u51b2\u7a81\u3002<\/p>\n<pre class=\"code\"><span style=\"color: green\">\/\/\u6211\u4eec\u6253\u5f00\u4e00\u4e2a\u65b0\u7684\u8fde\u63a5\u6765\u6a21\u62df\u53e6\u5916\u4e00\u4e2a\u7528\u6237\r\n<\/span><span style=\"color: #2b91af\">NorthwindDataContext <\/span>otherUser_db = <span style=\"color: blue\">new <\/span><span style=\"color: #2b91af\">NorthwindDataContext<\/span>();\r\n<span style=\"color: blue\">var <\/span>otherUser_product =\r\notherUser_db.Products.First(p =&gt; p.ProductID == 1);\r\notherUser_product.UnitPrice = 999.99M;\r\notherUser_db.SubmitChanges();\r\n<span style=\"color: green\">\/\/\u6211\u4eec\u5f53\u524d\u8fde\u63a5\r\n<\/span><span style=\"color: blue\">var <\/span>product = db.Products.First(p =&gt; p.ProductID == 1);\r\nproduct.UnitPrice = 777.77M;\r\n<span style=\"color: blue\">try\r\n<\/span>{\r\ndb.SubmitChanges();<span style=\"color: green\">\/\/\u5f53\u524d\u8fde\u63a5\u6267\u884c\u6210\u529f\r\n<\/span>}\r\n<span style=\"color: blue\">catch <\/span>(<span style=\"color: #2b91af\">ChangeConflictException<\/span>)\r\n{\r\n}<\/pre>\n<p>\u8bf4\u660e\uff1a\u6211\u4eec\u8bfb\u53d6\u6570\u636e\u4e4b\u540e\uff0c\u53e6\u5916\u4e00\u4e2a\u7528\u6237\u83b7\u53d6\u5e76\u63d0\u4ea4\u66f4\u65b0\u4e86\u8fd9\u4e2a\u6570\u636e\uff0c\u8fd9\u65f6\uff0c\u6211\u4eec\u66f4\u65b0\u8fd9\u4e2a\u6570\u636e\u65f6\uff0c\u5f15\u8d77\u4e86\u4e00\u4e2a\u5e76\u53d1\u51b2\u7a81\u3002\u7cfb\u7edf\u53d1\u751f\u56de\u6eda\uff0c\u5141\u8bb8\u4f60\u627e\u56de\u6700\u8fd1\u66f4\u65b0\u7684\u6570\u636e\uff0c\u8ba9\u4f60\u51b3\u5b9a\u7ee7\u7eed\u4f60\u7684\u66f4\u65b0\u3002<\/p>\n<pre class=\"code\"><span style=\"color: green\">\/\/\u5f53\u524d\u7528\u6237\r\n<\/span><span style=\"color: blue\">var <\/span>product = db.Products.First(p =&gt; p.ProductID == 1);\r\n<span style=\"color: green\">\/\/\u6211\u4eec\u6253\u5f00\u4e00\u4e2a\u65b0\u7684\u8fde\u63a5\u6765\u6a21\u62df\u53e6\u5916\u4e00\u4e2a\u7528\u6237\r\n<\/span><span style=\"color: #2b91af\">NorthwindDataContext <\/span>otherUser_db = <span style=\"color: blue\">new <\/span><span style=\"color: #2b91af\">NorthwindDataContext<\/span>() ;\r\n<span style=\"color: blue\">var <\/span>otherUser_product =\r\notherUser_db.Products.First(p =&gt; p.ProductID == 1);\r\notherUser_product.UnitPrice = 999.99M;\r\notherUser_db.SubmitChanges();\r\n<span style=\"color: green\">\/\/\u5f53\u524d\u7528\u6237\u4fee\u6539\r\n<\/span>product.UnitPrice = 777.77M;\r\n<span style=\"color: blue\">try\r\n<\/span>{\r\ndb.SubmitChanges();\r\n}\r\n<span style=\"color: blue\">catch <\/span>(<span style=\"color: #2b91af\">ChangeConflictException<\/span>)\r\n{\r\n<span style=\"color: green\">\/\/\u53d1\u751f\u5f02\u5e38\uff01\r\n<\/span>}<\/pre>\n<h1 class=\"hcode\"><font size=\"4\">Transactions\u4e8b\u52a1<\/font><\/h1>\n<p>LINQ to SQL \u652f\u6301\u4e09\u79cd\u4e8b\u52a1\u6a21\u578b\uff0c\u5206\u522b\u662f\uff1a<\/p>\n<ul>\n<li>\u663e\u5f0f\u672c\u5730\u4e8b\u52a1\uff1a\u8c03\u7528 SubmitChanges \u65f6\uff0c\u5982\u679c Transaction \u5c5e\u6027\u8bbe\u7f6e\u4e3a\u4e8b\u52a1\uff0c\u5219\u5728\u540c\u4e00\u4e8b\u52a1\u7684\u4e0a\u4e0b\u6587\u4e2d\u6267\u884c SubmitChanges \u8c03\u7528\u3002\u6210\u529f\u6267\u884c\u4e8b\u52a1\u540e\uff0c\u8981\u7531\u60a8\u6765\u63d0\u4ea4\u6216\u56de\u6eda\u4e8b\u52a1\u3002\u4e0e\u4e8b\u52a1\u5bf9\u5e94\u7684\u8fde\u63a5\u5fc5\u987b\u4e0e\u7528\u4e8e\u6784\u9020 DataContext \u7684\u8fde\u63a5\u5339\u914d\u3002\u5982\u679c\u4f7f\u7528\u5176\u4ed6\u8fde\u63a5\uff0c\u5219\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002<\/li>\n<li>\u663e\u5f0f\u53ef\u5206\u53d1\u4e8b\u52a1\uff1a\u53ef\u4ee5\u5728\u5f53\u524d Transaction \u7684\u4f5c\u7528\u57df\u4e2d\u8c03\u7528 LINQ to SQL API\uff08\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e SubmitChanges\uff09\u3002LINQ to SQL \u68c0\u6d4b\u5230\u8c03\u7528\u662f\u5728\u4e8b\u52a1\u7684\u4f5c\u7528\u57df\u5185\uff0c\u56e0\u800c\u4e0d\u4f1a\u521b\u5efa\u65b0\u7684\u4e8b\u52a1\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c&lt;token&gt;vbtecdlinq&lt;\/token&gt; \u8fd8\u4f1a\u907f\u514d\u5173\u95ed\u8fde\u63a5\u3002\u60a8\u53ef\u4ee5\u5728\u6b64\u7c7b\u4e8b\u52a1\u7684\u4e0a\u4e0b\u6587\u4e2d\u6267\u884c\u67e5\u8be2\u548c SubmitChanges \u64cd\u4f5c\u3002<\/li>\n<li>\u9690\u5f0f\u4e8b\u52a1\uff1a\u5f53\u60a8\u8c03\u7528 SubmitChanges \u65f6\uff0cLINQ to SQL \u4f1a\u68c0\u67e5\u6b64\u8c03\u7528\u662f\u5426\u5728 Transaction \u7684\u4f5c\u7528\u57df\u5185\u6216\u8005 Transaction \u5c5e\u6027\u662f\u5426\u8bbe\u7f6e\u4e3a\u7531\u7528\u6237\u542f\u52a8\u7684\u672c\u5730\u4e8b\u52a1\u3002\u5982\u679c\u8fd9\u4e24\u4e2a\u4e8b\u52a1\u5b83\u5747\u672a\u627e\u5230\uff0c\u5219 LINQ to SQL \u542f\u52a8\u672c\u5730\u4e8b\u52a1\uff0c\u5e76\u4f7f\u7528\u6b64\u4e8b\u52a1\u6267\u884c\u6240\u751f\u6210\u7684 SQL \u547d\u4ee4\u3002\u5f53\u6240\u6709 SQL \u547d\u4ee4\u5747\u5df2\u6210\u529f\u6267\u884c\u5b8c\u6bd5\u65f6\uff0cLINQ to SQL \u63d0\u4ea4\u672c\u5730\u4e8b\u52a1\u5e76\u8fd4\u56de\u3002<\/li>\n<\/ul>\n<h2>1.Implicit\uff08\u9690\u5f0f\uff09<\/h2>\n<p>\u8bf4\u660e\uff1a\u8fd9\u4e2a\u4f8b\u5b50\u5728\u6267\u884cSubmitChanges()\u64cd\u4f5c\u65f6\uff0c\u9690\u5f0f\u5730\u4f7f\u7528\u4e86\u4e8b\u52a1\u3002\u56e0\u4e3a\u5728\u66f4\u65b02\u79cd\u4ea7\u54c1\u7684\u5e93\u5b58\u6570\u91cf\u65f6\uff0c\u7b2c\u4e8c\u4e2a\u4ea7\u54c1\u5e93\u5b58\u6570\u91cf\u4e3a\u8d1f\u6570\u4e86\u3002\u8fd9\u5bfc\u81f4\u4e86\u66f4\u65b0\u4ea7\u54c1\u5168\u90e8\u5931\u8d25\u4e86\uff0c\u7cfb\u7edf\u56de\u6eda\u5230\u8fd9\u4e2a\u64cd\u4f5c\u7684\u521d\u59cb\u72b6\u6001\u3002<\/p>\n<pre class=\"code\"><span style=\"color: blue\">try\r\n<\/span>{\r\n<span style=\"color: #2b91af\">Product <\/span>prod1 = db.Products.First(p =&gt; p.ProductID == 4);\r\n<span style=\"color: #2b91af\">Product <\/span>prod2 = db.Products.First(p =&gt; p.ProductID == 5);\r\nprod1.UnitsInStock -= 3;\r\nprod2.UnitsInStock -= 5;<span style=\"color: green\">\/\/\u9519\u8bef:\u5e93\u5b58\u6570\u91cf\u7684\u5355\u4f4d\u4e0d\u80fd\u662f\u8d1f\u6570\r\n\/\/\u8981\u4e48\u5168\u90e8\u6210\u529f\u8981\u4e48\u5168\u90e8\u5931\u8d25\r\n<\/span>db.SubmitChanges();\r\n}\r\n<span style=\"color: blue\">catch <\/span>(System.Data.SqlClient.<span style=\"color: #2b91af\">SqlException <\/span>e)\r\n{\r\n<span style=\"color: green\">\/\/\u6267\u884c\u5f02\u5e38\u5904\u7406\r\n<\/span>}<\/pre>\n<h2>2.Explicit\uff08\u663e\u5f0f\uff09<\/h2>\n<p>\u8bf4\u660e\uff1a\u8fd9\u4e2a\u4f8b\u5b50\u4f7f\u7528\u4e8b\u52a1\u5c01\u95ed\u6570\u636e\u63d0\u4ea4\u3002<\/p>\n<pre class=\"code\"><span style=\"color: blue\">using <\/span>(<span style=\"color: #2b91af\">TransactionScope <\/span>ts = <span style=\"color: blue\">new <\/span><span style=\"color: #2b91af\">TransactionScope<\/span>())\r\n{\r\n<span style=\"color: blue\">try\r\n<\/span>{\r\n<span style=\"color: #2b91af\">Product <\/span>prod1 = db.Products.First(p =&gt; p.ProductID == 4);\r\n<span style=\"color: #2b91af\">Product <\/span>prod2 = db.Products.First(p =&gt; p.ProductID == 5);\r\nprod1.UnitsInStock -= 3;\r\nprod2.UnitsInStock -= 5;<span style=\"color: green\">\/\/\u9519\u8bef:\u5e93\u5b58\u6570\u91cf\u7684\u5355\u4f4d\u4e0d\u80fd\u662f\u8d1f\u6570\r\n<\/span>db.SubmitChanges();\r\n}\r\n<span style=\"color: blue\">catch <\/span>(System.Data.SqlClient.<span style=\"color: #2b91af\">SqlException <\/span>e)\r\n{\r\n<span style=\"color: green\">\/\/\u6267\u884c\u5f02\u5e38\u5904\u7406\r\n<\/span>}\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5fae\u8f6f\u4e8e2\u67081\u65e5\u53d1\u5e03\u4e86Visual Studio Team Syste&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-129","post","type-post","status-publish","format-standard","hentry","category-NET"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=129"}],"version-history":[{"count":0,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/129\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}