Esempio n. 1
0
 public Athlete SaveAthlete(Athlete athlete)
 {
     using (var db = new Context())
     {
         db.Entry(athlete).State = athlete.Id == 0 ? EntityState.Added : EntityState.Modified;
         db.SaveChanges();
     }
     return athlete;
 }
Esempio n. 2
0
        private static void InsertAthletes()
        {
            var sw = new Stopwatch();
            var ef = new EFAccessor();
            var dapper = new DapperAccessor();

            var athlete = new Athlete
            {
                FirstName = Guid.NewGuid().ToString(),
                LastName = Guid.NewGuid().ToString(),
                Position = Guid.NewGuid().ToString(),
            };

            sw.Start();
            ef.SaveAthlete(athlete);
            sw.Stop();
            Console.WriteLine("EF save Athlete first: {0}", sw.ElapsedMilliseconds);

            athlete = new Athlete
            {
                FirstName = Guid.NewGuid().ToString(),
                LastName = Guid.NewGuid().ToString(),
                Position = Guid.NewGuid().ToString(),
            };

            sw.Restart();
            ef.SaveAthlete(athlete);
            sw.Stop();
            Console.WriteLine("EF save Athlete second: {0}", sw.ElapsedMilliseconds);

            athlete = new Athlete
            {
                FirstName = Guid.NewGuid().ToString(),
                LastName = Guid.NewGuid().ToString(),
                Position = Guid.NewGuid().ToString(),
            };
            sw.Restart();
            dapper.SaveAthlete(athlete);
            sw.Stop();
            Console.WriteLine("Dapper save Athlete first: {0}", sw.ElapsedMilliseconds);

            athlete = new Athlete
            {
                FirstName = Guid.NewGuid().ToString(),
                LastName = Guid.NewGuid().ToString(),
                Position = Guid.NewGuid().ToString(),
            };
            sw.Restart();
            dapper.SaveAthlete(athlete);
            sw.Stop();
            Console.WriteLine("Dapper save Athlete second: {0}", sw.ElapsedMilliseconds);
        }
Esempio n. 3
0
        public Athlete SaveAthlete(Athlete athlete)
        {
            using (var conn = new SqlConnection(_ConnectionString))
            {
                conn.Open();

                if (athlete.Id == 0)
                {
                    athlete.Id = conn.Insert(athlete);
                }
                else
                {
                    conn.Update(athlete);
                }

                return athlete;
            }
        }
Esempio n. 4
0
        private void SeedAthletes()
        {
            var footballPositions = new string[] { "Quarter Back", "Wide Receiver", "Safety", "Punter" };
            var baseballPositions = new string[] { "First Base", "Right Field", "Catcher", "Pitcher" };
            var soccerPositons = new string[] { "Forward", "Midfield", "Defense", "Keeper" };

            Athlete athlete;
            using (var db = new Context())
            {
                List<Athlete> footballAthletes = new List<Athlete>();
                for (int x = 0; x < 1000; x++)
                {
                    athlete = new Athlete
                    {
                        FirstName = Guid.NewGuid().ToString(),
                        LastName = Guid.NewGuid().ToString(),
                        Position = footballPositions[x % footballPositions.Length]
                    };
                   footballAthletes.Add(db.Athletes.Add(athlete));
                }
                db.SaveChanges();

                var athleteIds = footballAthletes.Select(ath => ath.Id);
                var teamId = Teams.First(team => team.Name == "Football").Id;
                foreach (var id in athleteIds)
                {
                    db.AthleteTeam.Add(new AthleteTeam
                    {
                        TeamId = teamId,
                        AthleteId = id,
                    });
                }
                db.SaveChanges();

                var baseballAthletes = new List<Athlete>();
                for (int x = 0; x < 1000; x++)
                {
                    athlete = new Athlete
                    {
                        FirstName = Guid.NewGuid().ToString(),
                        LastName = Guid.NewGuid().ToString(),
                        Position = baseballPositions[x % baseballPositions.Length]
                    };
                   baseballAthletes.Add(db.Athletes.Add(athlete));
                }
                db.SaveChanges();

                athleteIds = footballAthletes.Select(ath => ath.Id);
                teamId = Teams.First(team => team.Name == "Baseball").Id;
                foreach (var id in athleteIds)
                {
                    db.AthleteTeam.Add(new AthleteTeam
                    {
                        TeamId = teamId,
                        AthleteId = id,
                    });
                }
                db.SaveChanges();

                var soccerAthletes = new List<Athlete>();
                for (int x = 0; x < 1000; x++)
                {
                    athlete = new Athlete
                    {
                        FirstName = Guid.NewGuid().ToString(),
                        LastName = Guid.NewGuid().ToString(),
                        Position = soccerPositons[x % baseballPositions.Length]
                    };
                    soccerAthletes.Add(db.Athletes.Add(athlete));
                }
                db.SaveChanges();

                athleteIds = footballAthletes.Select(ath => ath.Id);
                teamId = Teams.First(team => team.Name == "Soccer").Id;
                foreach (var id in athleteIds)
                {
                    db.AthleteTeam.Add(new AthleteTeam
                    {
                        TeamId = teamId,
                        AthleteId = id,
                    });
                }
                db.SaveChanges();
            }
        }