【问题标题】:EntityFramework.BulkInsert is not giving any warnings or errors but isn't savingEntityFramework.BulkInsert 没有给出任何警告或错误,但没有保存
【发布时间】:2016-03-16 20:36:55
【问题描述】:

我过去曾使用过 BulkInsert,但这是我没有经历过的。这是正在发生的事情。

我正在创建一个List<tEntityType>,在我生成我的列表之后,我调用context.BulkInsert(myList);。 Visual Studio 没有给我任何错误,当我运行代码并在调用上设置断点时,它似乎执行批量插入并继续。但是,当我浏览基础表时,里面没有数据?

明确地说,这是代码优先,SQL CompactSQL Express 2014 数据库。此外,在同一事务范围内,我将记录插入到其他数据表中并且它们正在出现。所以事务正在执行,但是,我的批量插入记录没有显示在数据库中。

我希望我能提供更多详细信息,但我没有收到任何错误、异常或警告。有没有人看到这个并想出了解决方案?

更多详情

如果您需要查看代码,这是我的代码的缩写。我只删除了一些逻辑来构建基础数据并更改了一些名称以“保护无辜者”。然而,这就是我正在做的所有事情。

        using (var ctx = new MyDbContainer())
        {
            using (var tx = new TransactionScope())
            {
                Header hr = new Header{Name= "NewRecord"};
                ctx.Headers.Add(hr);
                ctx.SaveChanges();

                //Get some records to work on and bulk insert.
                List<Records> recs = new List<Records>();

                //Records are inserted into the Recs list collection...

                //The purpose of this is to show that other records are being added within the same TX and are being committed to the db on the SaveChanges() call.  These records will be in the db afterwards.
                ctx.OtherRecords.Add(new OtherRecord { /* Details... */ } );
                ctx.OtherRecords.Add(new OtherRecord { /* Details... */ } );
                ctx.OtherRecords.Add(new OtherRecord { /* Details... */ } );

                //Do the BulkInsert and save the data.
                ctx.BulkInsert(recs.ToList());
                ctx.SaveChanges();
                tx.Complete();
            }
        }

如上所述,我的批量插入是一个非常大的过程的一部分。首先,我插入一个标题记录。我这样做是为了可以在我的其他记录中使用它的 ID 字段作为外键值。我一次插入几条记录,然后调用批量插入。

数据库正在保存 HeaderRecord 以及 OtherRecords。我可以看到数据。但是,批量插入的记录不在数据库中。会发生什么?

更新

我从 SQL Compact 迁移到 SQL Server Express 2014。这仍然没有解决问题。最离奇的是,我无法让异常发生并给我更多的细节。

【问题讨论】:

  • 如果您遍历 rateHistory 并将它们全部添加,然后调用 SaveChanges 是否有效?这并不能解决 BulkInsert 问题,但我很好奇至少有这么多工作。

标签: c# entity-framework entity-framework-6 ef-bulkinsert


【解决方案1】:

EntityFramework.BulkInsert NuGet 包似乎还不支持 SQL Server Compact。

如 CodePlex 网站上的 discussion 所示:

尚不支持 SQL Server CE。它可能会在下一个版本中添加。

不过,EntityFramework.BulkInsert.SqlServerCe NuGet 包已提供此功能。

【讨论】:

  • 不好意思拿走答题。我搬到了 SQL Server Express 2014,但它仍然无法正常工作。另外,上面提到的 NuGet 包也不起作用。
猜你喜欢
  • 1970-01-01
  • 2019-01-28
  • 1970-01-01
  • 2020-11-30
  • 2018-07-21
  • 1970-01-01
  • 1970-01-01
  • 2012-06-09
  • 1970-01-01
相关资源
最近更新 更多