public Athlete[] FindByPosition(string position) { using (var db = new Context()) { return db.Set<Athlete>().Where(athlete => athlete.Position.Equals(position, StringComparison.OrdinalIgnoreCase)).ToArray(); } }
public Athlete FindAthlete(long athleteId) { using (var db = new Context()) { return db.Set<Athlete>().Find(athleteId); } }
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; }
public void Seed() { using (var db = new Context()) { if (db.Set<Athlete>().Count() == 0) { SeedTeams(); SeedAthletes(); } } }
public TeamAthletes FindTeamWithAthletes(long teamId) { using (var db = new Context()) { var team = db.Set<Team>().Find(teamId); var teamathletes = ( from athletes in db.Set<Athlete>() join at in db.Set<AthleteTeam>() on athletes.Id equals at.AthleteId where at.TeamId == teamId select athletes ).ToArray(); return new TeamAthletes { Team = team, Athletes = teamathletes, }; } }
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(); } }
private void SeedTeams() { var teams = new Team[] { new Team { Name = "Football" }, new Team { Name= "Baseball" }, new Team { Name = "Soccer" } }; using (var db = new Context()) { foreach (var team in teams) { Teams.Add(db.Set<Team>().Add(team)); db.SaveChanges(); } } }
private static void LoadTeamWithAthletes() { var sw = new Stopwatch(); var ef = new EFAccessor(); var dapper = new DapperAccessor(); long[] teamIds; using (var db = new Context()) { teamIds = db.Set<Team>().Select(team => team.Id).ToArray(); } sw.Start(); ef.FindTeamWithAthletes(teamIds[0]); sw.Stop(); Console.WriteLine("EF find Team with Athletes first: {0}", sw.ElapsedMilliseconds); sw.Restart(); ef.FindTeamWithAthletes(teamIds[1]); sw.Stop(); Console.WriteLine("EF find Team with Athletes second: {0}", sw.ElapsedMilliseconds); sw.Restart(); dapper.FindTeamWithAthletes(teamIds[2]); sw.Stop(); Console.WriteLine("Dapper find Team with Athletes first: {0}", sw.ElapsedMilliseconds); sw.Restart(); dapper.FindTeamWithAthletes(teamIds[0]); sw.Stop(); Console.WriteLine("Dapper find Team with Athletes second: {0}", sw.ElapsedMilliseconds); }