Batch / bulk insert problem (EF Core)
Posted: Tue 06 Nov 2018 10:27
Hi
Can't get batch-insert working on EF Core (transaction). The child-tables are inserted one-by-one with a RETURNING (according to dbMonitor).
In my inherit class:
It must be in a transaction (according to the first DELETE).
BUT dbMonitor shows lots of INSERT's (with RETURNING id and timestamp) instead of COPY. Why????
Can't get batch-insert working on EF Core (transaction). The child-tables are inserted one-by-one with a RETURNING (according to dbMonitor).
In my inherit class:
Code: Select all
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UsePostgreSql( connectionString);
PgSqlEntityProviderConfig config = PgSqlEntityProviderConfig.Instance;
config.DmlOptions.BatchUpdates.Enabled = true;
config.DmlOptions.BatchUpdates.BatchSize = 100;
}
Code: Select all
var pgMon = new PgSqlMonitor();
pgMon.IsActive = true;
using (var myContext = new MyDataModel("User Id=...;Password=...;Host=...;Database=...;Unicode=True;Persist Security Info=True;Initial Schema=..."))
{
myContext.Database.BeginTransaction();
// DELETE
myContext.myTable1.RemoveRange(myContext.myTable1
.Where(s => s.t1Id == "00001"));
myContext.SaveChanges();
// INSERT (bulk/batch)
// ...
// myData is one record of table1 and multiple one-to-many relation to table2 (and table3). All tables has autogenerated ID's and TIMESTAMPs (when inserted).
//
myContext.myTable1.Add(myData);
int c1 = myContext.SaveChanges();
myContext.Database.CommitTransaction();
pgMon.IsActive = false;
pgMon.Dispose();
}