public static List <AuditEntry> UseAudit(int nbRecords, StringBuilder sb) { using (var ctx = new CodeFirstEntities()) { DbHelper.SetDemoEntities(ctx, sb, nbRecords); var auditEntries = new List <AuditEntry>(); ctx.BulkSaveChanges(operation => { operation.UseAudit = true; // Multiple bulk operations can be executed in a BulkSaveChanges event, the BulkOperationExecuted action // will be invoked after each bulk operations. // // Audit entries contain information about: // - Table // - Operation // - Date // - Old Column Value // - New Column Value operation.BulkOperationExecuted += bulkOperation => auditEntries.AddRange(bulkOperation.AuditEntries); }); return(auditEntries); } }
public static List <AuditEntry> UseLogDump(int nbRecords, StringBuilder sb) { using (var ctx = new CodeFirstEntities()) { DbHelper.SetDemoEntities(ctx, sb, nbRecords); var auditEntries = new List <AuditEntry>(); ctx.BulkSaveChanges(operation => { operation.UseLogDump = true; // Multiple bulk operations can be executed in a BulkSaveChanges event, the BulkOperationExecuted action // will be invoked after each bulk operations. // // The log dump contains all database events. // - A connection is opened // - A command is executed // - A SqlBulkCopy is executed operation.BulkOperationExecuted += bulkOperation => sb.AppendLine(bulkOperation.LogDump.ToString()); }); return(auditEntries); } }
public static void BulkSaveChanges2(int nbRecords, Stopwatch clock, StringBuilder sb) { BulkOperationManager.BulkOperationBuilder = operation => operation.BatchSize = 1000; using (var ctx = new CodeFirstEntities()) { // Do any kind of change to your context! ctx.EntitySimples.ToList().ForEach(x => x.ColumnInt = x.ColumnInt + 1); // ctx.SaveChanges(); ctx.BulkSaveChanges(operation => operation.BatchSize = 50); // Save 50x faster using BulkSaveChanges } }
public static void Log(int nbRecords, StringBuilder sb) { using (var ctx = new CodeFirstEntities()) { DbHelper.SetDemoEntities(ctx, sb, nbRecords); // The log event is invoked every time a database event occurs. // - A connection is opened // - A command is executed // - A SqlBulkCopy is executed ctx.BulkSaveChanges(operation => { operation.Log += s => sb.AppendLine(s); }); } }
public static void RetryInterval(int nbRecords, StringBuilder sb) { using (var ctx = new CodeFirstEntities()) { DbHelper.SetDemoEntities(ctx, sb, nbRecords); ctx.BulkSaveChanges(operation => { operation.RetryCount = 3; // RetryInterval property allows to wait X ms before retrying the operation when a transient error occurs. // Transient error: https://msdn.microsoft.com/en-us/library/4cff491e-9359-4454-bd7c-fb72c4c452ca#bkmk_connection_errors operation.RetryInterval = new TimeSpan(0, 0, 1); }); } }
public static void BulkSaveChanges(int nbRecords, Stopwatch clock, StringBuilder sb) { using (var ctx = new CodeFirstEntities()) { List <EntitySimple> list = ctx.EntitySimples.Take(nbRecords / 2).ToList(); int recordToUpdate = list.Count / 2; int recordToDelete = list.Count - recordToUpdate; int recordToInsert = nbRecords - recordToUpdate - recordToDelete; // Insert { var listToInsert = new List <EntitySimple>(); for (int i = 0; i < recordToInsert; i++) { listToInsert.Add(new EntitySimple { ColumnInt = i % 5 }); } ctx.EntitySimples.AddRange(listToInsert); } // Update { List <EntitySimple> listToUpdate = list.Take(recordToUpdate).ToList(); listToUpdate.ForEach(x => x.ColumnInt = x.ColumnInt + 1); } // Delete { List <EntitySimple> listToDelete = list.Skip(recordToUpdate).ToList(); ctx.EntitySimples.RemoveRange(listToDelete); } sb.Append(string.Format("INSERT {0} / UPDATE {1} / DELETE {2} entities", recordToInsert, recordToUpdate, recordToDelete)); clock.Start(); ctx.BulkSaveChanges(); clock.Stop(); } }