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 BulkMerge(int nbRecords, Stopwatch clock, StringBuilder sb)
        {
            var recordsToUpdate = nbRecords/2;
            var recordsToInsert = nbRecords - recordsToUpdate;

            var listToInsert = new List<EntitySimple>();

            for (var i = 0; i < recordsToInsert; i++)
            {
                listToInsert.Add(new EntitySimple {ColumnInt = i%5});
            }

            using (var ctx = new CodeFirstEntities())
            {
                ctx.EntitySimples.AddRange(listToInsert);

                var listToUpdate = ctx.EntitySimples.Take(recordsToUpdate).AsNoTracking().ToList();
                listToUpdate.ForEach(x => x.ColumnInt = x.ColumnInt + 1);

                var listToMerge = listToInsert.ToList();
                listToMerge.AddRange(listToUpdate);

                sb.Append(string.Format("INSERT {0} / UPDATE {1} entities", recordsToInsert, recordsToUpdate));

                clock.Start();
                ctx.BulkMerge(listToMerge);
                clock.Stop();
            }
        }
        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 BulkDelete(int nbRecords, Stopwatch clock, StringBuilder sb)
        {
            using (var ctx = new CodeFirstEntities())
            {
                List<EntitySimple> list = ctx.EntitySimples.Take(nbRecords).AsNoTracking().ToList();

                sb.Append(string.Format("DELETE {0} entities", list.Count));

                clock.Start();
                ctx.BulkDelete(list);
                clock.Stop();
            }
        }
예제 #6
0
        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 SaveChangesUpdate(int nbRecords, Stopwatch clock, StringBuilder sb)
        {
            using (var ctx = new CodeFirstEntities())
            {
                List<EntitySimple> list = ctx.EntitySimples.Take(nbRecords).ToList();
                list.ForEach(x => x.ColumnInt = x.ColumnInt + 1);

                sb.Append(string.Format("UPDATE {0} entities", list.Count));

                clock.Start();
                ctx.SaveChanges();
                clock.Stop();
            }
        }
        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);
                });
            }
        }
        protected void Application_Start(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(My.Config.AppSettings.LicenseName))
            {
                LicenseManager.AddLicense(My.Config.AppSettings.LicenseName, My.Config.AppSettings.LicenseKey);
            }

            // DO a warmup call for JIT compilation
            using (var ctx = new CodeFirstEntities())
            {
                ctx.EntitySimples.Add(new EntitySimple {ColumnInt = 1});
                ctx.SaveChanges();

                ctx.EntitySimples.Add(new EntitySimple {ColumnInt = 1});
                ctx.BulkSaveChanges();
            }
        }
        public static void SaveChangesInsert(int nbRecords, Stopwatch clock, StringBuilder sb)
        {
            var list = new List<EntitySimple>();

            for (int i = 0; i < nbRecords; i++)
            {
                list.Add(new EntitySimple {ColumnInt = i%5});
            }

            using (var ctx = new CodeFirstEntities())
            {
                ctx.EntitySimples.AddRange(list);

                sb.Append(string.Format("INSERT {0} entities", list.Count));

                clock.Start();
                ctx.SaveChanges();
                clock.Stop();
            }
        }
예제 #11
0
        protected void Application_Start(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(My.Config.AppSettings.LicenseName))
            {
                LicenseManager.AddLicense(My.Config.AppSettings.LicenseName, My.Config.AppSettings.LicenseKey);
            }

            // DO a warmup call for JIT compilation
            using (var ctx = new CodeFirstEntities())
            {
                ctx.EntitySimples.Add(new EntitySimple {
                    ColumnInt = 1
                });
                ctx.SaveChanges();

                ctx.EntitySimples.Add(new EntitySimple {
                    ColumnInt = 1
                });
                ctx.BulkSaveChanges();
            }
        }
        public static void SetDemoEntities(CodeFirstEntities ctx, StringBuilder sb, int nbRecords)
        {
            var recordsToUpdate = nbRecords/2;
            var recordsToInsert = nbRecords - recordsToUpdate;

            var listToInsert = new List<EntitySimple>();

            for (var i = 0; i < recordsToInsert; i++)
            {
                listToInsert.Add(new EntitySimple {ColumnInt = i%5});
            }

            ctx.EntitySimples.AddRange(listToInsert);

            var listToUpdate = ctx.EntitySimples.Take(recordsToUpdate).ToList();
            listToUpdate.ForEach(x => x.ColumnInt = x.ColumnInt + 1);

            var listToMerge = listToInsert.ToList();
            listToMerge.AddRange(listToUpdate);

            sb.Append(string.Format("INSERT {0} / UPDATE {1} entities", recordsToInsert, recordsToUpdate));
        }
        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();
            }
        }