I am attempting to use the EntityFramework.BulkInsert library for taking advantage of EF6 and SqlBulkCopy.
The documentation seems very straight forward, however, I cannot get any data populated into the database.
For a complex example, here is the code:
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();
}
}
}
This code will basically insert into a global list until the list count is divisible by chunckCount.
The AddRange function works just fine. So, we decided to create a very simple database with a single table called "Test" and here is the issue we ran into:
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 believe these are separate issues but maybe someone in the stackoverflow community has any idea on why we are having this issue.
So, after awhile of attempting to solve the problem, the solution was fairly straight forward. The same rules apply for the SqlBulkCopy where each table has to be inserted, not just the table that has references to other tables (via foreign keys). Very fast though!