EntityFramework.BulkInsert problems inserting into database
I am trying to use the EntityFramework.BulkInsert library to use EF6 and SqlBulkCopy.
The documentation seems to be very simple, however, I cannot get the data entered into the database.
For a complex example, here's 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 stick to the global list until the number of lists is divisible by chunckCount.
The AddRange function works fine. So, we decided to create a very simple database with one table "Test", and here is the problem 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 perhaps someone from the stackoverflow community has an idea of why we are having this problem.
So after a while, trying to solve the problem, the solution was pretty simple. The same rules apply for SqlBulkCopy, where every table must be inserted, not just a table that has links to other tables (via foreign keys). Very fast though!
I was able to solve this problem by creating a Code First EF6 project. This is a solution, but the first model would be nice.