Introduction
I am trying to seed a SQLite DB with Entity Framework but it's unacceptably slow.
I have an entity MyEntity
with 5 columns which has a composite key consists of 3 columns.
Initial approach
dbContext.MyEntity.AddOrUpdate(x => new{x.Key1, x.Key2, x.Key3}, myEntities.ToArray());
This works incredibly slow. 1138 rows in 40 seconds.
Another Try
Then using this, this and this I came up with this block:
using (var scope = new TransactionScope())
{
DatabaseContext dbContext;
using (dbContext = new DatabaseContext())
{
dbContext.Database.Connection.Open();
for (int i = 0; i < myEntities.Count; i++)
{
dbContext.MyEntity.AddOrUpdate(
x => new
{
x.Key1,
x.Key2,
x.Key3
},
myEntities.ElementAt(i));
if (i % 100 == 0)
{
dbContext.SaveChanges();
dbContext.Dispose();
dbContext = new DatabaseContext();
dbContext.Database.Connection.Open();
}
}
dbContext.SaveChanges();
dbContext.Dispose();
}
scope.Complete();
}
Sadly, I get:
Message: Test method threw exception: System.Data.SQLite.SQLiteException: database is locked database is locked
Trying with BulkExtensions library
I also tried BulkExtensions library like this: dbContext.BulkInsertOrUpdate(myEntities)
resulted with following exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Data.SQLite.SQLiteException: database schema has changed near "0": syntax error
I believe this happens because I haven't provided the information (there is no way to provide) of the composite key.
The Question
How can I seed a SQLite DB in a reasonable time?