Example #1
0
        public static long Delete(MsDbCtx db, Person[] allPersons)
        {
            Console.WriteLine("Delete...");
            var allPersonData = allPersons.SelectMany(s => s.PersonData).ToArray();
            var stopwatch     = new Stopwatch();

            stopwatch.Start();
            // Get All Data from DB
            if (SingleQuery)
            {
                db.PersonData.RemoveRange(allPersonData);
                db.Person.RemoveRange(allPersons);
                db.SaveChanges();
            }
            else
            {
                Array.ForEach(allPersonData, personData =>
                {
                    var p = db.PersonData.Single(s => s.Id == personData.Id);
                    db.PersonData.Remove(p);
                    db.SaveChanges();
                });
                Array.ForEach(allPersons, person =>
                {
                    var p = db.Person.Single(s => s.Id == person.Id);
                    db.Person.Remove(p);
                    db.SaveChanges();
                });
            }
            stopwatch.Stop();
            return(stopwatch.ElapsedMilliseconds);
        }
Example #2
0
        public static long Create(MsDbCtx db, DataModel[] dataModels)
        {
            Console.WriteLine("Create...");
            var stopwatch = new Stopwatch();

            // Start to Insert Data to MS SQL Server
            if (SingleQuery)
            {
                stopwatch.Start();

                var persons = dataModels.Select(s => new Person {
                    Name = s.Name
                }).ToArray();
                db.Person.AddRange(persons);
                db.SaveChanges();

                for (int i = 0; i < persons.Length; i++)
                {
                    db.PersonData.AddRange(dataModels[i].Values.Select(s => new PersonData {
                        PersonId = persons[i].Id, Value = s
                    }).ToArray());
                }
                db.SaveChanges();
            }
            else
            {
                stopwatch.Start();
                Array.ForEach(dataModels, item =>
                {
                    var person = new Person {
                        Name = item.Name
                    };
                    db.Person.Add(person);
                    db.SaveChanges();

                    Array.ForEach(item.Values, value =>
                    {
                        db.PersonData.Add(new PersonData
                        {
                            PersonId = person.Id,
                            Value    = value
                        });
                        db.SaveChanges();
                    });
                });
            }

            stopwatch.Stop();
            return(stopwatch.ElapsedMilliseconds);
        }
Example #3
0
        public static long Update(MsDbCtx db, Person[] allPersons)
        {
            Console.WriteLine("Update...");
            var allPersonData = allPersons.SelectMany(s => s.PersonData).ToArray();
            var stopwatch     = new Stopwatch();

            stopwatch.Start();
            // Get All Data from DB
            if (SingleQuery)
            {
                Array.ForEach(allPersons, person =>
                {
                    person.Name += "*";
                    Array.ForEach(person.PersonData.ToArray(), data =>
                    {
                        data.Value += 1.1;
                    });
                });
                db.SaveChanges();
            }
            else
            {
                Array.ForEach(allPersons, person =>
                {
                    var p   = db.Person.Single(s => s.Id == person.Id);
                    p.Name += "*";
                    db.SaveChanges();
                });
                Array.ForEach(allPersonData, personData =>
                {
                    var p    = db.PersonData.Single(s => s.Id == personData.Id);
                    p.Value += 0.1;
                    db.SaveChanges();
                });
            }
            stopwatch.Stop();
            return(stopwatch.ElapsedMilliseconds);
        }