EntityFramework.BulkInsert没有给出任何警告或错误但是没有保存

时间:2016-03-16 20:36:55

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

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

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

要清楚,这是一个代码, SQL Compact SQL Express 2014数据库。此外,在相同的事务范围内,我将记录插入到其他数据表中并且它们正在出现。因此,事务正在执行,但是我的批量插入记录不会显示在数据库中。

我希望我能提供更多细节,但我没有收到任何错误,例外或警告。有没有人看过这个并想出一个解决方案?

更多详情

如果您需要查看代码,这是我的代码的缩写。我只删除了一些逻辑来构建基础数据,并将一些名称改为&#34;保护无辜者。&#34;然而,这就是我所做的一切。

        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。这仍然没有解决问题。最离奇的是我无法获得异常并给我更多细节。

1 个答案:

答案 0 :(得分:1)

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

如CodePlex网站上的discussion所示:

  

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

但是,EntityFramework.BulkInsert.SqlServerCe NuGet包已经提供了此功能。