public void ProcessBatch(List <TIn> items, TCtx dbContext, BulkLoadMode bulkLoadMode) { switch (bulkLoadMode) { case BulkLoadMode.None: items.ForEach(i => dbContext.Add(i)); dbContext.SaveChanges(); //DO NOT SET IT ASYNC HERE. The point is to retrieve the Id in case of automatic key. break; case BulkLoadMode.InsertOnly: dbContext.BulkInsert(items, new BulkConfig { PreserveInsertOrder = true, SetOutputIdentity = true, BatchSize = items.Count }); break; case BulkLoadMode.Upsert: dbContext.BulkInsertOrUpdate(items, new BulkConfig { PreserveInsertOrder = true, SetOutputIdentity = true, BatchSize = items.Count }); break; default: break; } }
public static IKeyedStream <TIn, TKey> ToEntityFrameworkCore <TIn, TRes, TKey>(this IKeyedStream <TIn, TKey> stream, string name, IStream <TRes> resourceStream, BulkLoadMode bulkLoadMode = BulkLoadMode.InsertOnly, int chunkSize = 1000) where TRes : DbContext where TIn : class { return(new ToEntityFrameworkCoreStreamNode <TIn, TRes, IKeyedStream <TIn, TKey> >(name, new ToEntityFrameworkCoreArgs <TIn, TRes, IKeyedStream <TIn, TKey> > { SourceStream = stream, DbContextStream = resourceStream, BatchSize = chunkSize, BulkLoadMode = bulkLoadMode }).Output); }