public static async Task Import() { var credentials = MongoCredential.CreateCredential(DbName, User, Pass); var settings = new MongoClientSettings { Server = new MongoServerAddress(DbHost, DbPort), Credentials = new List <MongoCredential> { credentials } }; var client = new MongoClient(settings); var mongoDb = client.GetDatabase(DbName); var dbContext = new NBAStatisticsDbContext(); await Task.Run(async() => { var mongoSeasons = mongoDb.GetCollection <NBAStatistics.Data.FillMongoDB.Models.Season>("Seasons").AsQueryable().ToList(); // Load all teams from the database into the context dbContext.Teams.Load(); foreach (var season in mongoSeasons) { foreach (var team in season.Teams) { //var teamInDb = dbContext.Teams.Local // .FirstOrDefault(t => t.TeamId == team.TeamId); var teamInDb = dbContext.Teams.Local .SingleOrDefault(t => t.TeamId == team.TeamId); // runs in memory if (teamInDb == null) { dbContext.Teams.Add(new Team { TeamId = team.TeamId, Name = team.Name }); } } } await dbContext.SaveChangesAsync(); var mongoPlayers = mongoDb.GetCollection <NBAStatistics.Data.FillMongoDB.Models.Player>("Players").AsQueryable().ToList(); // Load all players from the database into the context dbContext.Players.Load(); dbContext.Teams.Load(); foreach (var player in mongoPlayers) { var playerNameParts = player.PlayerName.Split(new char[] { ' ' }); // throws an exception if there is more than 1 element in the sequence var playerInDb = dbContext.Players.Local .SingleOrDefault(p => p.AdditionalInfo.PlayerId == player.PlayerId); // runs in memory if (playerInDb == null) { dbContext.Players.Add(new Player { FirstName = playerNameParts.Length > 0 ? playerNameParts[0] : "", LastName = playerNameParts.Length > 1 ? playerNameParts[1] : "", AdditionalInfo = new PlayerInfo { PlayerId = player.PlayerId, Birthday = DateTime.ParseExact(player.BirthDate, "MMM dd, yyyy", CultureInfo.InvariantCulture), Height = ConvertHeightFromFeetsInchesToCentimeters(player.Height), Weight = PoundsToKilogram(player.Weight) }, School = string.IsNullOrEmpty(player.School) ? null : new School { Name = player.School }, Position = player.Position, TeamId = dbContext.Teams.Local .Single(t => t.TeamId == player.TeamId) .Id }); } } await dbContext.SaveChangesAsync(); }); }
public static async Task ImportFromMongoDB() { var credentials = MongoCredential.CreateCredential(DbName, User, Pass); var settings = new MongoClientSettings { Server = new MongoServerAddress(DbHost, DbPort), Credentials = new List <MongoCredential> { credentials } }; var client = new MongoClient(settings); var mongoDb = client.GetDatabase(DbName); var dbContext = new NBAStatisticsDbContext(); await Task.Run(async() => { var mongoSeasons = mongoDb.GetCollection <NBAStatistics.Data.FillMongoDB.Models.Season>("Seasons").AsQueryable().ToList(); // Load all teams from the database into the dbContext dbContext.Teams.Load(); var nullTeamInDb = dbContext.Teams.Local .SingleOrDefault(t => t.TeamId == 0); // runs in memory if (nullTeamInDb == null) { // add fake team for players without team dbContext.Teams.Add(new Team { TeamId = 0, Name = "NoName", Abbreviation = "", Founded = 0, City = new City { Name = "NoName", Country = new Country { Name = "NoName" } }, Arena = new Arena { Name = "NoName" }, HeadCoach = new HeadCoach { Name = "NoName" } }); } foreach (var season in mongoSeasons) { foreach (var team in season.Teams) { //var teamInDb = dbContext.Teams.Local // .FirstOrDefault(t => t.TeamId == team.TeamId); var teamInDb = dbContext.Teams.Local .SingleOrDefault(t => t.TeamId == team.TeamId); // runs in memory if (teamInDb == null) { dbContext.Teams.Add(new Team { TeamId = team.TeamId, Name = team.Name, Abbreviation = team.Abbreviation, Founded = team.Founded, City = new City { Name = team.City, Country = new Country { Name = team.Country } }, Arena = new Arena { Name = team.Arena }, HeadCoach = new HeadCoach { Name = team.HeadCoach } }); } } } await dbContext.SaveChangesAsync(); var mongoPlayers = mongoDb.GetCollection <NBAStatistics.Data.FillMongoDB.Models.Player>("Players").AsQueryable().ToList(); // Load all players from the database into the dbContext dbContext.Players.Load(); dbContext.Teams.Load(); foreach (var playerMongoDB in mongoPlayers) { // throws an exception if there is more than 1 element in the sequence var playerInDb = dbContext.Players.Local .SingleOrDefault(p => p.AdditionalInfo.PlayerId == playerMongoDB.PlayerId); // runs in memory if (playerInDb == null) { var player = new Player { FirstName = playerMongoDB.FirstName, LastName = playerMongoDB.LastName, AdditionalInfo = new PlayerInfo { PlayerId = playerMongoDB.PlayerId, Birthday = playerMongoDB.BirthDate, Height = string.IsNullOrEmpty(playerMongoDB.Height) ? null : ConvertHeightFromFeetsInchesToCentimeters(playerMongoDB.Height), Weight = string.IsNullOrEmpty(playerMongoDB.Weight) ? null : PoundsToKilogram(playerMongoDB.Weight) }, School = string.IsNullOrEmpty(playerMongoDB.School) ? null : new School { Name = playerMongoDB.School }, Country = string.IsNullOrEmpty(playerMongoDB.Country) ? new Country { Name = "NoName" } : string.IsNullOrEmpty(playerMongoDB.Country.Trim()) ? new Country { Name = "NoName" } : new Country { Name = playerMongoDB.Country }, Position = playerMongoDB.Position, RosterStatus = playerMongoDB.RosterStatus, TeamId = dbContext.Teams.Local .Single(t => t.TeamId == playerMongoDB.TeamId) .Id }; dbContext.Players.Local.Add(player); } } await dbContext.SaveChangesAsync(); // Load all players and seasons from the database into the dbContext dbContext.Players.Load(); dbContext.Seasons.Load(); dbContext.PlayerSeasonPointsPerGame.Load(); foreach (var playerMongoDB in mongoPlayers) { var player = dbContext.Players.Local .FirstOrDefault(p => p.AdditionalInfo.PlayerId == playerMongoDB.PlayerId); foreach (var sppg in playerMongoDB.SeasonPointsPerGame) { var seasonIdInMongoDB = sppg.Key; var pointsPerGameInMongoDB = sppg.Value; var seasonId = dbContext.Seasons.Local .FirstOrDefault(s => s.SeasonId == seasonIdInMongoDB) .Id; var playerSeasonPointsPerGameRecordExists = dbContext.PlayerSeasonPointsPerGame.Local .Where(ps => ps.PlayerId == player.Id && ps.SeasonId == seasonId) .Count() > 0; if (playerSeasonPointsPerGameRecordExists) { // skip this record to DB continue; } var playerSeasonPointsPerGame = new PlayerSeasonPointsPerGame { PlayerId = player.Id, SeasonId = seasonId, PointsPerGame = pointsPerGameInMongoDB }; player.PlayerSeasonPointsPerGame.Add(playerSeasonPointsPerGame); } } await dbContext.SaveChangesAsync(); }); }