EntityFramework.BulkInsert issues inserting into database

bulkinsert entity-framework sqlbulkcopy

Question

In an effort to use EF6 and SqlBulkCopy, I'm trying to use the EntityFramework.BulkInsert library.

Although the instructions appear to be quite clear, I am unable to fill the database with any data.

The code for a challenging example is as follows:

public void WriteChunkNoAsync(int chunkCount, bool forceSave = false)
{
    Chunking.Entities.Add(this);
    if (forceSave || Chunking.Entities.Count % chunkCount == 0)
    {
        using (var db = new FlatESContainer())
        {
            //db.Entity.AddRange(Chunking.Entities); //This will work
            db.BulkInsert(Chunking.Entities);
            Chunking.Entities = new List<Entity>();
            db.SaveChanges();
        }
    }
}

Basically, this code will continue to add to a global list until the list count equals chunckCount.

The AddRange function works well. So, we made the decision to build a very basic database with only one table, "Test," and this is where we ran into trouble:

List<Test> tests = new List<Test>();
for (int i = 0; i < 1000; i++)
{
    tests.Add(new Test());
}

using (var context = new SimpleContainer())
{
    //This works fine
    context.Tests.AddRange(tests);
    //This causes an exception: Type 'TestSimpleDatabase.Test' is not found in context 'TestSimpleDatabase.SimpleContainer'
    context.BulkInsert(tests);
    context.SaveChanges();
}

I think these are different problems, but maybe someone in the stackoverflow community knows why we are seeing this problem.

1
3
8/26/2014 3:58:04 PM

Popular Answer

So, after some time spent trying to find a solution, one seemed very obvious. The SqlBulkCopy follows the same guidelines in that all tables must be added, not only those that have references to other tables (via foreign keys). Very quickly, however!

1
8/27/2014 1:12:05 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow