{"id":9646,"date":"2019-01-18T16:00:18","date_gmt":"2019-01-18T07:00:18","guid":{"rendered":"http:\/\/www.moonmile.net\/blog\/?p=9646"},"modified":"2019-01-18T16:42:13","modified_gmt":"2019-01-18T07:42:13","slug":"linq-%e3%81%ae-insert-%e3%81%8c%e9%81%85%e3%81%84%e3%81%a8%e3%81%8d%e3%81%af-autodetectchangesenabled-%e3%82%92-false-%e3%81%ab%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"http:\/\/www.moonmile.net\/blog\/archives\/9646","title":{"rendered":"LINQ \u306e INSERT \u304c\u9045\u3044\u3068\u304d\u306f AutoDetectChangesEnabled \u3092 False \u306b\u3059\u308b"},"content":{"rendered":"<p>\u3068\u3042\u308b\u30b7\u30b9\u30c6\u30e0\u30671\u4e07\u4ef6\u7a0b\u5ea6\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u69cb\u6210\u3057\u306a\u304a\u3057\u3066\u5225\u306e\u8907\u6570\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u79fb\u3059\u3053\u3068\u3092\u3084\u3063\u3066\u3044\u305f\u3002\u3044\u308f\u3086\u308b\u3001\u6b63\u898f\u5316\u3057\u3066\u3044\u306a\u3044\u30c6\u30fc\u30d6\u30eb\u3092\u30c7\u30fc\u30bf\u79fb\u884c\u306e\u969b\u306b\u6b63\u898f\u5316\u3057\u3088\u3046\u3068\u601d\u3063\u3066\u3001\u8907\u6570\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u5206\u3051\u305f\u306e\u3060\u304c\u3001\u305f\u304b\u3060\u304b1\u4e07\u4ef6\u3057\u304b\u306a\u3044\u306e\u306b\u975e\u5e38\u306b\u9045\u3044\u3002\u6b63\u898f\u5316\u306e\u30ed\u30b8\u30c3\u30af\u304c\u9045\u3044\u306e\u304b\u3082\u3057\u308c\u306a\u3044\u3051\u3069\u30011\u4e07\u4ef6\u7a0b\u5ea6\u3092\u30c7\u30fc\u30bf\u633f\u5165\u3059\u308b\u306e\u306b30\u5206\u4f4d\u639b\u304b\u3063\u3066\u3057\u307e\u3046\u306e\u3067\u3042\u308b\u3002<br \/>\n\u76f8\u624b\u304c SQL Server \u306a\u306e\u3067 SqlBulkCopy \u3092\u4f7f\u3048\u3070\u7d50\u69cb\u306a\u30b9\u30d4\u30fc\u30c9\u306b\u306a\u308b\u306f\u305a\u306a\u306e\u3060\u304c\u3001100\u4e07\u4ef6\u306e\u5834\u5408\u306a\u3089\u3070\u305d\u3046\u304b\u3082\u3057\u308c\u306a\u3044\u3051\u3069\u3001\u305f\u304b\u3060\u304b1\u4e07\u4ef6\u306e\u633f\u5165\u3067\u3053\u3093\u306a\u306b\u9045\u3044\u306e\u306f\u5909\u3002\u3068\u3044\u3046\u3053\u3068\u3067\u3001LINQ \u306e INSERT \u306b\u3064\u3044\u3066\u8abf\u3079\u306a\u304a\u3057\u3066\u307f\u308b\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u7d50\u8ad6\u304b\u3089\u8a00\u3048\u3070\u3001AutoDetectChangesEnabled \u3068 ValidateOnSaveEnabled \u3092 OFF(false) \u306b\u3059\u308c\u3070\u3088\u3044\u3002<\/p>\n<p>DbContextConfiguration.AutoDetectChangesEnabled Property<br \/>\n<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.data.entity.infrastructure.dbcontextconfiguration.autodetectchangesenabled?redirectedfrom=MSDN&amp;view=entity-framework-6.2.0#overloads\">https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.data.entity.infrastructure.dbcontextconfiguration.autodetectchangesenabled?redirectedfrom=MSDN&amp;view=entity-framework-6.2.0#overloads<\/a><\/p>\n<p>ent.Configuration.AutoDetectChangesEnabled = false;<br \/>\nent.Configuration.ValidateOnSaveEnabled = false;<\/p>\n<p>LINQ \u3067 INSERT\/DELETE\/UPDATE \u3092\u3059\u308b\u5834\u5408\u3001EF \u306e\u5185\u90e8\u3067\u6574\u5408\u6027\u3092\u30c1\u30a7\u30c3\u30af\u3057\u3066\u3044\u308b\u3002System.Data.Entity.DbSet.Add \u306a\u3069\u3092\u547c\u3073\u51fa\u3057\u305f\u3068\u304d\u306b\u3001DetectChanges() \u3067\u30c1\u30a7\u30c3\u30af\u3092\u3057\u3066\u3044\u308b\u3068\u3044\u3046\u308f\u3051\u3060\u3002\u3069\u3046\u3084\u3089\u3053\u308c\u304c\u9045\u3044\u539f\u56e0\u306a\u306e\u3067\u3001\u7d20\u76f4\u306b System.Data.Entity.Infrastructure.DbContextConfiguration.AutoDetectChangesEnabled \u306e\u5024\u3092 false \u306b\u3057\u3066\u547c\u3073\u51fa\u3055\u306a\u3044\u3088\u3046\u306b\u3059\u308c\u3070\u3088\u3044\u3002<br \/>\n\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f\u3001AutoDetectChangesEnabled \u304c true \u3068\u306a\u3063\u3066\u3044\u308b\u3002<\/p>\n<h2>\u5b9f\u9a13<\/h2>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nCREATE TABLE &#x5B;dbo].&#x5B;BulkT](\r\n\t&#x5B;ID] &#x5B;int] IDENTITY(1,1) NOT NULL,\r\n\t&#x5B;GUID] &#x5B;varchar](100) NOT NULL,\r\n\t&#x5B;Created] &#x5B;datetime] NOT NULL\r\n)\r\n<\/pre>\n<p>\u3053\u306e BulkT \u30c6\u30fc\u30d6\u30eb\u306b1\u4e07\u4ef6\u306e\u30c7\u30fc\u30bf\u3092\u633f\u5165\u3059\u308b\u3002SaveChanges \u30921\u56de\u306e\u633f\u5165\u3054\u3068\u306b\u884c\u3063\u3066\u3044\u308b\u304c\u3001\u30c6\u30fc\u30d6\u30eb\u304c\u8907\u96d1\u306a\u5834\u5408\u306f\u6700\u5f8c\u306b1\u56de\u3060\u3051\u3060\u3068\u30e1\u30e2\u30ea\u3092\u98df\u3044\u3059\u304e\u305f\u308a\u3059\u308b\u305f\u3081\u3001\u4f55\u5ea6\u304b\u306b\u5206\u3051\u308b(100\u56de\u6bce\u306a\u3069\uff09\u5fc5\u8981\u304c\u3042\u308b\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprivate void clickNormal(object sender, RoutedEventArgs e)\r\n{\r\n    var ent = new testdbEntities();\r\n    ent.Database.ExecuteSqlCommand(&amp;quot;delete BulkT&amp;quot;);\r\n    var start = DateTime.Now;\r\n    for ( int i=0; i&lt;10000; i++ )\r\n    {\r\n        var t = new BulkT()\r\n        {\r\n            GUID = Guid.NewGuid().ToString(&amp;quot;N&amp;quot;),\r\n            Created = DateTime.Now,\r\n        };\r\n        ent.BulkT.Add(t);\r\n        ent.SaveChanges();\r\n    }\r\n    var tend = DateTime.Now;\r\n    var span = (tend - start).TotalSeconds;\r\n    System.Diagnostics.Debug.WriteLine(span.ToString());\r\n}\r\n\r\nprivate void clickNoAutoDetect(object sender, RoutedEventArgs e)\r\n{\r\n    var ent = new testdbEntities();\r\n    ent.Database.ExecuteSqlCommand(&amp;quot;delete BulkT&amp;quot;);\r\n    var start = DateTime.Now;\r\n    ent.Configuration.AutoDetectChangesEnabled = false;\r\n    ent.Configuration.ValidateOnSaveEnabled = false;\r\n    for (int i = 0; i &lt; 10000; i++)\r\n    {\r\n        var t = new BulkT()\r\n        {\r\n            GUID = Guid.NewGuid().ToString(&amp;quot;N&amp;quot;),\r\n            Created = DateTime.Now,\r\n        };\r\n        ent.BulkT.Add(t);\r\n        ent.SaveChanges();\r\n    }\r\n    var tend = DateTime.Now;\r\n    var span = (tend - start).TotalSeconds;\r\n    System.Diagnostics.Debug.WriteLine(span.ToString());\r\n}\r\n<\/pre>\n<p>DetectChanges \u306e\u30c1\u30a7\u30c3\u30af\u3042\u308a\uff08AutoDetectChangesEnabled = true\uff09<br \/>\n50.1799167<br \/>\n48.9856614<br \/>\n48.5278795<\/p>\n<p>DetectChanges \u306e\u30c1\u30a7\u30c3\u30af\u7121\u3057\uff08AutoDetectChangesEnabled = false)<br \/>\n6.1251368<br \/>\n6.1039912<br \/>\n6.2945836<\/p>\n<p>\u3053\u306e\u3088\u3046\u306b8\u500d\u3050\u3089\u3044\u306e\u5dee\u304c\u3067\u3066\u304f\u308b\u3002100\u4ef6\u7a0b\u5ea6\u306a\u3089\u3070\u7279\u306b\u554f\u984c\u3082\u3067\u306a\u3044\u3060\u308d\u3046\u304c\u30011\u4e07\u4ef6\u4ee5\u4e0a\u3042\u308b\u5834\u5408\u306f\u6c17\u3092\u4ed8\u3051\u305f\u304a\u3044\u305f\u307b\u3046\u304c\u3088\u3044\u3060\u308d\u3046\u3002\u30c7\u30fc\u30bf\u30c1\u30a7\u30c3\u30af\u304c\u5165\u3089\u306a\u3044\u306e\u3067\u3001\u633f\u5165\u30c7\u30fc\u30bf\u306b\u6c17\u3092\u4ed8\u3051\u308b\u5fc5\u8981\u304c\u3042\u308b\u304c\u3001\u4eca\u56de\u306e\u3088\u3046\u306b\u7a7a\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u633f\u5165\u3059\u308b\u5834\u5408\u306f\u30d7\u30ed\u30b0\u30e9\u30e0\u5185\u3067\u30c1\u30a7\u30c3\u30af\u304c\u6e08\u3093\u3067\u3044\u308b\u306e\u3067\u7279\u306b\u554f\u984c\u306f\u306a\u3044\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u300130\u5206\u4ee5\u4e0a\u639b\u304b\u3063\u3066\u3044\u305f\u30c7\u30fc\u30bf\u30b3\u30f3\u30d0\u30fc\u30c8\u306f1\u5206\u4ee5\u5185\u306b\u7d42\u308f\u308b\u3088\u3046\u306b\u306a\u3063\u305f\u3002<\/p>\n<h2>\u53c2\u8003\u30ea\u30f3\u30af<\/h2>\n<p>Entity Framework \u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9 #2 \u66f4\u65b0\u51e6\u7406 | C#.NET vs VB.NET<br \/>\n<a href=\"http:\/\/csharpvbcomparer.blogspot.com\/2015\/04\/net-ef-performance-2-updating.html\">http:\/\/csharpvbcomparer.blogspot.com\/2015\/04\/net-ef-performance-2-updating.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3068\u3042\u308b\u30b7\u30b9\u30c6\u30e0\u30671\u4e07\u4ef6\u7a0b\u5ea6\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u69cb\u6210\u3057\u306a\u304a\u3057\u3066\u5225\u306e\u8907\u6570\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u79fb\u3059\u3053\u3068\u3092\u3084\u3063\u3066\u3044\u305f\u3002\u3044\u308f\u3086\u308b\u3001\u6b63\u898f\u5316\u3057\u3066\u3044\u306a\u3044\u30c6\u30fc\u30d6\u30eb\u3092\u30c7\u30fc\u30bf\u79fb\u884c\u306e\u969b\u306b\u6b63\u898f\u5316\u3057\u3088\u3046\u3068\u601d\u3063\u3066\u3001\u8907\u6570\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u5206\u3051\u305f\u306e\u3060\u304c\u3001\u305f\u304b\u3060\u304b1\u4e07\u4ef6\u3057\u304b\u306a\u3044 &hellip; <a href=\"http:\/\/www.moonmile.net\/blog\/archives\/9646\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,23],"tags":[],"class_list":["post-9646","post","type-post","status-publish","format-standard","hentry","category-dev","category-csharp"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/9646","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/comments?post=9646"}],"version-history":[{"count":2,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/9646\/revisions"}],"predecessor-version":[{"id":9648,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/9646\/revisions\/9648"}],"wp:attachment":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/media?parent=9646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/categories?post=9646"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/tags?post=9646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}