{"id":10623,"date":"2021-10-15T10:54:35","date_gmt":"2021-10-15T01:54:35","guid":{"rendered":"https:\/\/www.moonmile.net\/blog\/?p=10623"},"modified":"2021-10-15T23:03:23","modified_gmt":"2021-10-15T14:03:23","slug":"net-core%e3%81%a7null%e4%bb%98%e3%81%8d%e3%81%ae%e3%83%87%e3%83%bc%e3%82%bf%e3%82%92sql-server%e3%81%ab%e5%a4%a7%e9%87%8f%e6%8a%95%e5%85%a5%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/www.moonmile.net\/blog\/archives\/10623","title":{"rendered":".NET Core\u3067NULL\u4ed8\u304d\u306e\u30c7\u30fc\u30bf\u3092SQL Server\u306b\u5927\u91cf\u6295\u5165\u3059\u308b"},"content":{"rendered":"\n<p>2\u5e74\u307b\u3069\u524d\u3001LINQ \u3092\u4f7f\u3063\u3066\u5927\u91cf\u30c7\u30fc\u30bf\u3092 INSERT \u3059\u308b\u3068\u9045\u3059\u304e\u308b\u306e\u3067\u3001LINQ \u306e INSERT \u3092 <a href=\"http:\/\/www.moonmile.net\/blog\/archives\/9649\">SqlBulkCopy \u306b\u3059\u308b\u3068\u3069\u308c\u3060\u3051\u65e9\u304f\u306a\u308b\u306e\u304b\uff1f | Moonmile Solutions Blog<\/a> \u3067\u3001SqlBulkCopy \u3092\u8a66\u3057\u307e\u3057\u305f\u3002\u3053\u306e\u8a18\u4e8b\u306e\u4f8b\u3067\u306f\u30016\u79d2\u304b\u3089 0.06 \u79d2\u306b\u306a\u3063\u3066 100 \u500d\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u5b9f\u969b\u306f\u3001\u4ed5\u4e8b\u3067\u4f7f\u3046\u30c7\u30fc\u30bf\u79fb\u884c\u5206\u304c\u3042\u3063\u3066\u30012\u4e07\u4ef6\u307b\u3069\u304c1\u6642\u9593\u3050\u3089\u3044\u304b\u304b\u3063\u3066\u3044\u305f\u306e\u304c\u3001\u6570\u79d2\u3067\u7d42\u308f\u308b\u3088\u3046\u306b\u52b9\u7387\u5316\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3053\u306e\u30c4\u30fc\u30eb\u81ea\u4f53\u306f .NET Framework \u306e EF6 \u3067\u66f8\u3044\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u73fe\u5728 .NET5 \u306e EF Core \u3067\u66f8\u304d\u63db\u3048\u4e2d\u3067\u3059\u3002\u3044\u307e\u307e\u3067\u3001EF6 \u3092\u4f7f\u3063\u3066 Visual Studio \u4e0a\u3067 Model \u30af\u30e9\u30b9\u3092\u4f5c\u3063\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001EF Core \u306e\u307b\u3046\u3067\u30b3\u30fc\u30c9\u30d5\u30a1\u30fc\u30b9\u30c8\u7684\u306b\u30c6\u30c6\u30fc\u30d6\u30eb\u7528\u306b\u5bfe\u5fdc\u3059\u308b\u30af\u30e9\u30b9\u3092\u4f5c\u3063\u3066\u3044\u307e\u3059\u3002\u30b3\u30fc\u30c9\u30d5\u30a1\u30fc\u30b9\u30c8\u3068\u306f\u3044\u3048\u3001\u5b9f\u969b\u306b\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u53cd\u6620\u3057\u3066\u306f\u3044\u307e\u305b\u3093\u3002\u3057\u304b\u3057\u3001\u3044\u3061\u3044\u3061 Visual Studio \u4e0a\u3067\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u5185\u306e\u30c6\u30fc\u30d6\u30eb\u3068\u306e\u540c\u671f\u3092\u3068\u3089\u306a\u304f\u3066\u3088\u3044\u306e\u3067\u697d\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u307e\u3042\u3001\u305d\u308c\u306f\u305d\u308c\u3067\u3001\u624b\u4f5c\u696d\u3067\u30c6\u30fc\u30d6\u30eb\u3092\u66f8\u304d\u63db\u3048\u3066\u306f\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3059\u308b\u308f\u3051\u3067\u3059\u304c\u3002<\/p>\n\n\n\n<p>\u3055\u3066\u3001EF Core \u306e INSERT \u3082 EF6 \u306e INSERT \u3068\u540c\u3058\u3088\u3046\u306b\u304b\u306a\u308a\u9045\u3044\u3067\u3059\u3002EF6 \u306e\u5834\u5408\u306f\u3001<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>AutoDetectChangesEnabled<\/li><li>ValidateOnSaveEnabled<\/li><\/ul>\n\n\n\n<p>\u3092 false \u306b\u3059\u308b\u3053\u3068\u3067\u3001INSERT \u306e\u9ad8\u901f\u5316\u304c\u305d\u3053\u305d\u3053\u3067\u304d\u308b\uff085\u500d\u3050\u3089\u3044\u65e9\u304f\u306a\u308b\uff09\u306e\u3067\u3059\u304c\u3001\u6570\u4e07\u4ef6\u306e\u30c7\u30fc\u30bf\u3092\u6295\u5165\u3057\u3088\u3046\u3068\u3059\u308b\u7d50\u69cb\u304b\u304b\u308a\u307e\u3059\u3002\u3055\u3089\u306b\u3001\u624b\u5143\u306e\u30c7\u30fc\u30bf\u3067\u306f EF6 \u3088\u308a\u3082 EF Core \u306e INSERT \u304c 5\u500d\u3050\u3089\u3044\u9045\u3044\u306e\u3067\u3001\u3061\u3087\u3063\u3068\u5927\u304d\u3081\u306e\u30c7\u30fc\u30bf\u3060\u3068 INSERT \u3060\u3051\u3067\u306f\u5b9f\u7528\u7684\u306b\u7121\u7406\u3068\u3044\u3046\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u305d\u3053\u3067\u3001\u518d\u3073 SqlBulkCopy \u306e\u51fa\u756a\u306a\u306e\u3067\u3059\u3002\u4ee5\u524d\u66f8\u3044\u3066\u3044\u305f\u62e1\u5f35\u30e1\u30bd\u30c3\u30c9 AsDataTable \u3060\u3068 null \u8a31\u5bb9\u578b\u304c\u901a\u3089\u306a\u3044\u306e\u3067\u5c11\u3057\u66f8\u304d\u63db\u3048\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-csharp\" data-lang=\"C#\"><code>public static class DataTableExtenstions\n{\n    public static DataTable AsDataTable&lt;T&gt;(this DbSet&lt;T&gt; src) where T : class\n    {\n        return DataTableExtenstions.AsDataTable(src.Local);\n    }\n    public static DataTable AsDataTable&lt;T&gt;(this IEnumerable&lt;T&gt; src) where T : class\n    {\n        var properties = typeof(T).GetProperties();\n        var dest = new DataTable();\n        \/\/ \u30c6\u30fc\u30d6\u30eb\u30ec\u30a4\u30a2\u30a6\u30c8\u306e\u4f5c\u6210\n        foreach (var prop in properties)\n        {\n            DataColumn dc = new DataColumn();\n            dc.ColumnName = prop.Name;\n            if (prop.PropertyType.IsGenericType && \n                prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable&lt;&gt;))\n            {\n                dc.DataType = Nullable.GetUnderlyingType(prop.PropertyType);\n                dc.AllowDBNull = true;\n            } else\n            {\n                dc.DataType = prop.PropertyType;\n            }\n            dest.Columns.Add(dc);\n        }\n        \/\/ \u5024\u306e\u6295\u3052\u8fbc\u307f\n        foreach (var item in src)\n        {\n            var row = dest.NewRow();\n            foreach (var prop in properties)\n            {\n                var itemValue = prop.GetValue(item, new object[] { });\n                row[prop.Name] = itemValue ?? System.DBNull.Value;\n            }\n            dest.Rows.Add(row);\n        }\n        return dest;\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<p>\u30c7\u30fc\u30bf\u3092\u6295\u5165\u3059\u308b\u3068\u304d\u306b null \u304b\u3089 System.DBNull.Value \u306b\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-csharp\" data-lang=\"C#\"><code>private void blukSave&lt;T&gt;(string tablename, IEnumerable&lt;T&gt; items, bool keepid = true ) where T : class\n{\n    var cnstr = toEnt.Database.GetDbConnection().ConnectionString;\n    SqlBulkCopy bc;\n    if (keepid == true)\n    {\n        bc = new SqlBulkCopy(cnstr, SqlBulkCopyOptions.KeepIdentity);\n    }\n    else\n    {\n        bc = new SqlBulkCopy(cnstr);\n    }\n    bc.DestinationTableName = tablename;\n    var dt = items.AsDataTable();\n    bc.WriteToServer(dt);\n}\n<\/code><\/pre><\/div>\n\n\n\n<p>\u78ba\u304b\u3001\u4ee5\u524d\u306e BulkCopy \u306f ID \u3092\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u3057\u306a\u304b\u3063\u305f\u3088\u3046\u306a\u6c17\u304c\u3059\u308b\u306e\u3067\u3059\u304c\u3001\u73fe\u5728\u306e SqlBulkCopy \u306f ID \u3092\u633f\u5165\u6642\u306b\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u3002\u5927\u91cf\u30c7\u30fc\u30bf\u3092\u6295\u5165\u3059\u308b\u3068\u304d\u306f\u3001ID \u306f\u3042\u3089\u304b\u3058\u3081\u632f\u3063\u3066\u3042\u308b\u3053\u3068\u304c\u591a\u3044\uff08\u4ed6\u306e\u30c7\u30fc\u30bf\u304b\u3089\u79fb\u884c\u3059\u308b\u305f\u3081\uff09\u306e\u3067\u3001INSERT \u6642\u306b ID \u306e\u5024\u304c\u5909\u308f\u3089\u306a\u3044\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u30aa\u30d7\u30b7\u30e7\u30f3\u3067 SqlBulkCopyOptions.KeepIdentity \u3092\u3064\u3051\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u5b9f\u969b\u306e\u4f7f\u3044\u65b9\u306f\u3053\u3093\u306a\u611f\u3058\u3002\u4e88\u7d04\u30c6\u30fc\u30d6\u30eb\u306f\u5b9f\u306f ACCESS \u304b\u3089\u79fb\u884c\u3059\u308b\u30c7\u30fc\u30bf\u306a\u306e\u3067\u5927\u91cf\u306b Nullable \u304c\u5165\u3063\u3066\u3044\u307e\u3059\u3002\u3044\u3063\u305f\u3093 List \u306b\u305f\u3081\u8fbc\u3093\u3067\u304b\u3089\u3001BlukCopy \u3092\u884c\u3046\u306e\u3067\u4e00\u6642\u7684\u306b\u305f\u3081\u8fbc\u307e\u308c\u308b List \u306e\u30e1\u30e2\u30ea\u91cf\u304c\u5fc3\u914d\u3067\u3059\u304c\u3001\u307e\u3042\u3001\u5927\u4e08\u592b\u3067\u3057\u3087\u3046\u3002\u6700\u521d\u306e DropTable \u95a2\u6570\u306f\u3001\u5185\u90e8\u3067 TRUNCATE TABLE \u3092\u547c\u3073\u51fa\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-csharp\" data-lang=\"C#\"><code>public bool To\u4e88\u7d04()\n{\n    DropTable(&quot;\u4e88\u7d04&quot;);\n    var lst = new List&lt;\u4e88\u7d04&gt;();\n    foreach (var it in fromEnt.T_\u4e88\u7d04)\n    {\n        var t = new \u4e88\u7d04();\n        t.ID = it.KID;\n        t.\u9867\u5ba2ID = it.TID.Value;\n        t.\u9867\u5ba2SUBID = it.TIDa ?? 0;\n        t.\u4e88\u7d04\u8005 = it.\u4e88\u7d04\u8005;\n        ...\n        t.UpdateAt = DateTime.Now;\n\n        lst.Add(t);\n    }\n    this.blukSave(&quot;\u4e88\u7d04&quot;, lst);\n    return true;\n}\n<\/code><\/pre><\/div>\n\n\n\n<p>\u3053\u306e\u72b6\u614b\u3067\u30012\u4e07\u4ef6\u306e\u30c7\u30fc\u30bf\u3092\u6295\u5165\u3059\u308b\u3068\u6570\u79d2\u3067\u7d42\u308f\u308a\u307e\u3059\u3002EF Core \u306e LINQ \u306e INSERT \u3092\u4f7f\u3046\u3068\u30012\u6642\u9593\u3050\u3089\u3044\u304b\u304b\u308b\u306e\u3067\u3001\u3053\u308c\u306f\u5b9f\u7528\u7684\u3068\u306f\u8a00\u3048\u307e\u305b\u3093\u3002\u4f55\u304c\u9045\u3044\u306e\u304c\u3044\u307e\u3044\u3061\u4e0d\u660e\u3067\u3059\u306d\u3002\u3002\u3002<\/p>\n\n\n\n<p>\u4f59\u8ac7\u3067\u3059\u304c\u3001.NET Core \u306e DbContext \u3092\u4f7f\u3063\u305f\u3068\u304d\u306b\u3001AutoDetectChangesEnabled \u3084 ValidateOnSaveEnabled \u304c\u3042\u308a\u307e\u305b\u3093\u3002\u4ee3\u308f\u308a\u306b\u3001OnConfiguring \u3092\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\u3057\u3066 UseQueryTrackingBehavior \u3092\u4f7f\u3044\u307e\u3059\u3002\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0\u3092\u3057\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u3068\u3001\u591a\u5c11\u306f\u65e9\u304f\u306a\u308b\uff082,3\u500d\u3050\u3089\u3044\uff09\u306e\u3067\u3059\u304c\u3001SqlBulkCopy \u306e 100\u500d\u306b\u306f\u9060\u304f\u53ca\u3073\u307e\u305b\u3093\u3002 <\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-csharp\" data-lang=\"C#\"><code>public class KaigiDbContext : DbContext\n{\n    public KaigiDbContext()\n    {\n\n    }\n    public KaigiDbContext(DbContextOptions&lt;AccessDbContext&gt; options) : base(options)\n    {\n    }\n    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)\n    {\n        string cnstr = &quot;data source=.;initial catalog=\u4f1a\u8b70\u5ba4;integrated security=True&quot;;\n        optionsBuilder.UseSqlServer(cnstr);\n        optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);\n    }\n    public DbSet&lt;\u4e88\u7d04&gt; \u4e88\u7d04 { get; set; }\n...\n}\n<\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>2\u5e74\u307b\u3069\u524d\u3001LINQ \u3092\u4f7f\u3063\u3066\u5927\u91cf\u30c7\u30fc\u30bf\u3092 INSERT \u3059\u308b\u3068\u9045\u3059\u304e\u308b\u306e\u3067\u3001LINQ \u306e INSERT \u3092 SqlBulkCopy \u306b\u3059\u308b\u3068\u3069\u308c\u3060\u3051\u65e9\u304f\u306a\u308b\u306e\u304b\uff1f | Moonmile Solutions Blog \u3067 &hellip; <a href=\"https:\/\/www.moonmile.net\/blog\/archives\/10623\">\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],"tags":[],"class_list":["post-10623","post","type-post","status-publish","format-standard","hentry","category-dev"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/10623","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/comments?post=10623"}],"version-history":[{"count":5,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/10623\/revisions"}],"predecessor-version":[{"id":10632,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/10623\/revisions\/10632"}],"wp:attachment":[{"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/media?parent=10623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/categories?post=10623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/tags?post=10623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}