public static void CreateBallpark(BallparkDTO ballparkDTO) { // ballpark instance of Ballpark class in Retrosheet_Persist.Retrosheet var ballpark = convertToEntity(ballparkDTO); // entity data model //var dbCtx = new retrosheetDB(); var dbCtx = new retrosheetEntities(); dbCtx.Ballparks.Add(ballpark); try { dbCtx.SaveChanges(); } catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } } catch (Exception e) { string text; text = e.Message; } }
private static Ballpark convertToEntity(BallparkDTO ballparkDTO) { var ballpark = new Retrosheet_Persist.Ballpark(); ballpark.record_id = ballparkDTO.RecordID; ballpark.ballpark_id = ballparkDTO.ID; ballpark.name = ballparkDTO.Name; ballpark.aka = ballparkDTO.AKA; ballpark.city = ballparkDTO.City; ballpark.state = ballparkDTO.State; ballpark.start_date = ballparkDTO.StartDate; ballpark.end_date = ballparkDTO.EndDate; ballpark.league = ballparkDTO.League; ballpark.notes = ballparkDTO.Notes; return(ballpark); }
private static void ReadWriteBallparkFile() { string[] columnValue; string textLine = null; string textLine1 = null; string textLine2 = null; int doubleQuoteIndex = 0; DateTime dateTime; using (StreamReader reader = new StreamReader(@"C:\users\mmr\documents\retrosheet\ballparkcodes.txt")) { while (!reader.EndOfStream) { try { textLine = reader.ReadLine(); } catch (Exception e) { // Let the user know what went wrong. Console.WriteLine("The " + @"C:\users\mmr\documents\retrosheet\ballparkcodes.txt" + " file could not be read:"); Console.WriteLine(e.Message); Console.ReadLine(); } // skip the header record in the input file if (textLine.IndexOf("PARKID") == -1) { doubleQuoteIndex = textLine.IndexOf("\""); // there are notes available on the baseballcodes record which may contain // commas within the double quotes - leave the comma in the notes. if (doubleQuoteIndex > -1) { textLine1 = textLine.Substring(0, doubleQuoteIndex - 1); textLine2 = textLine.Substring(doubleQuoteIndex + 1); textLine1 = textLine1.Replace(",", "|"); textLine2 = textLine2.Replace("\"", ""); textLine = textLine1 + "|" + textLine2; } else { textLine = textLine.Replace(",", "|"); } columnValue = textLine.Split('|'); BallparkDTO ballparkDTO = new BallparkDTO(); //Retrosheet_Persist.Ballpark newBallpark = new Retrosheet_Persist.Ballpark(); ballparkDTO.RecordID = Guid.NewGuid(); ballparkDTO.ID = columnValue[0]; ballparkDTO.Name = columnValue[1]; ballparkDTO.AKA = columnValue[2]; ballparkDTO.City = columnValue[3]; ballparkDTO.State = columnValue[4]; if (DateTime.TryParse(columnValue[5], out dateTime)) { ballparkDTO.StartDate = dateTime; } else { ballparkDTO.StartDate = DateTime.MinValue; } if (DateTime.TryParse(columnValue[6], out dateTime)) { ballparkDTO.EndDate = dateTime; } else { ballparkDTO.EndDate = DateTime.MaxValue; } ballparkDTO.League = columnValue[7]; ballparkDTO.Notes = columnValue[8]; BallparkPersist.CreateBallpark(ballparkDTO); } Console.WriteLine(textLine); } } }