public static Course FindOrMake(this DbSet<Course> courses, CourseType type, Term term, string fourDigitYear, DbContext ctx) { int year; Course course = null; if (Int32.TryParse(fourDigitYear, out year) && year > 1900 && year < 2100) { course = courses.FirstOrDefault(c => c.CourseTypeCode == type.CourseTypeCode && c.TermId == term.TermId && c.Year == year); if (course == default(Course)) { course = new Course() { Term = term, CourseType = type, Year = year }; courses.Add(course); ctx.SaveChanges(); } } return (course); }
public static CourseType FindOrMake(this DbSet<CourseType> types, string code, string name, DbContext ctx) { var ct = types.FirstOrDefault(t => t.CourseTypeCode == code); if (ct == default(CourseType)) { ct = new CourseType() { CourseTypeName = name, CourseTypeCode = code }; types.Add(ct); ctx.SaveChanges(); } return (ct); }
static void Main(string[] args) { HibernatingRhinos.Profiler.Appender.EntityFramework.EntityFrameworkProfiler.Initialize(); using (var context = new SceneCRM()) { WipeDatabase(context); oneOnOne = context.CourseTypes.FindOrMake("OO", "One on One", context); pm1 = context.CourseTypes.FindOrMake("PM1", "Playmaking One", context); rp = context.CourseTypes.FindOrMake("RP", "Replay", context); s1 = context.CourseTypes.FindOrMake("S1", "Stage One", context); pb = context.CourseTypes.FindOrMake("PB", "Playback", context); PopulateJobTypes(context); var ss = new ChildrenProductionsSpreadsheet(@"C:\Users\dylan.beattie\Documents\Scene & Heard\Children and Productions.xls"); foreach (var row in ss.Rows) { var student = context.Students.FindOrMake(row.MembershipNumber, row.Forename, row.Surname, context); student.QuestionnaireResponse = row.QuestionnaireResponses; ImportPlaymakingOne(context, student, row); ImportPlayback(context, student, row); ImportReplay(context, student, row); ImportStageOne(context, student, row); ImportOneOnOne(context, student, row); Console.WriteLine("Added " + student.Forename + " " + student.Surname); context.SaveChanges(); } ImportVolunteerDataFromAccessDatabase(context, @"C:\Users\dylan.beattie\Documents\Scene & Heard\Volunteers.mdb", "giraffe"); } Console.ReadKey(false); }
static object ImportPlay(SceneCRM context, Student student, CourseType courseType, string termName, string termYear, string productionName, string playName, string dramaturg, string director, params string[] actors) { object returnValue = null; var term = context.Terms.FindOrMake(termName, context); var course = context.Courses.FindOrMake(courseType, term, termYear, context); if (course != null) { var attendance = new CourseAttendance() { Student = student, Course = course, Completed = true }; Production production = context.Productions.FindOrMake(productionName, context); if (!String.IsNullOrWhiteSpace(playName)) { var play = new Play() { Student = student, Title = playName }; if (production != null) play.Production = production; attendance.Play = play; student.Plays.Add(play); AddPlayVolunteer(context, play, dramaturg, Jobs.Dramaturg); AddPlayVolunteer(context, play, director, Jobs.Director); foreach (var actor in actors) { AddPlayVolunteer(context, play, actor, Jobs.Actor); } returnValue = play; } else { AddCourseVolunteer(context, course, dramaturg, Jobs.Dramaturg); AddCourseVolunteer(context, course, director, Jobs.Director); foreach (var actor in actors) { AddCourseVolunteer(context, course, actor, Jobs.Actor); } returnValue = course; } student.CourseAttendances.Add(attendance); } return(returnValue); }