public static void BulkMerge(int nbRecords, Stopwatch clock, StringBuilder sb)
        {
            int recordsToUpdate = nbRecords/2;
            int recordsToInsert = nbRecords - recordsToUpdate;

            var listToInsert = new List<EntitySimple>();

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

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

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

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

                clock.Start();
                ctx.BulkMerge(listToUpdate);
                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();
            }
        }
        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();
            }
        }
        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();
            }
        }
예제 #7
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 SaveChanges(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.SaveChanges();
                clock.Stop();
            }
        }