public void AddTeam(Team team) { using (var context = new HockeyContext()) { context.Database.Log = s => Debug.WriteLine(s); foreach (var player in team.Players) { if (player == null) throw new ArgumentNullException(nameof(player), "all players in a team must not be null"); var newPlayer = context.Players.FirstOrDefault(p => p.FirstName == player.FirstName && p.LastName == player.LastName); if (newPlayer != null) { player.Id = newPlayer.Id; context.Entry(newPlayer).State = EntityState.Detached; context.Players.Attach(player); } if (player.Id > 0) context.Players.Attach(player); foreach (var position in player.Positions) { context.Positions.Attach(position); } } context.Teams.Add(team); context.SaveChanges(); } }
public IEnumerable<Player> GetPlayers() { using (var context = new HockeyContext()) { var players = context.Players.Include(p => p.Positions).ToList(); return players; } }
public void AddPosition(string name) { using (var context = new HockeyContext()) { var newPosition = new Position { Name = name }; context.Positions.Add(newPosition); context.SaveChanges(); } }
public void DeleteAllData() { using (var context = new HockeyContext()) { var teams = context.Teams.Include(t => t.Players).ToList(); var players = context.Players.Include(p => p.Positions); context.Teams.RemoveRange(teams); context.Players.RemoveRange(players); context.SaveChanges(); } }
public void AddPlayer(Player player) { using (var context = new HockeyContext()) { //context.Database.Log = s => Debug.WriteLine(s); //if (player.Position?.Name != null && player.Position.Id == 0) // player.Position = context.Positions.FirstOrDefault(p => p.Name == player.Position.Name); context.Players.Add(player); context.SaveChanges(); } }
public IEnumerable<Position> GetPositions() { using (var context = new HockeyContext()) { var positions = context.Positions.ToList(); return positions; } }
public Position GetPosition(string positionName) { using (var context = new HockeyContext()) { var position = context.Positions.FirstOrDefault(p => p.Name == positionName); return position; } }
public IEnumerable<Team> GetTeams() { using (var context = new HockeyContext()) { var teams = context.Teams.Include(t => t.Players).ToList(); return teams; } }