示例#1
0
        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 SaveChangesDelete(int nbRecords, Stopwatch clock, StringBuilder sb)
        {
            using (var ctx = new CodeFirstEntities())
            {
                List <EntitySimple> list = ctx.EntitySimples.Take(nbRecords).ToList();
                ctx.EntitySimples.RemoveRange(list);

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

                clock.Start();
                ctx.SaveChanges();
                clock.Stop();
            }
        }
        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();
            }
        }
        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();
            }
        }