public static bool AllMigrationsApplied(this DSMSDBContext context) { var applied = context.GetService <IHistoryRepository>() .GetAppliedMigrations() .Select(m => m.MigrationId); var total = context.GetService <IMigrationsAssembly>() .Migrations .Select(m => m.Key); return(!total.Except(applied).Any()); }
/// <summary> /// Returns the number of school days in a given year. /// </summary> /// <param name="StartDate">The start date.</param> /// <returns></returns> public static int GetSchoolDayCount(DateTime?StartDate = null, DateTime?EndDate = null) { int retVal = 1; DateTime startDate; DateTime endDate; var context = new DSMSDBContext(); if (!StartDate.HasValue && !EndDate.HasValue && normalSchoolDayCount > 0) { return(normalSchoolDayCount); } if (StartDate.HasValue && StartDate.Value > context.SchoolStart.CalendarDateTime) { startDate = StartDate.Value; } else { startDate = context.CurrentCalendarEvents.FirstOrDefault(e => e.CalendarDateTime <= DateTime.Now && e.CalendarEventType == (int)CalendarEventType.FirstDayOfSchool).CalendarDateTime; } if (EndDate.HasValue && StartDate.Value > context.SchoolEnd.CalendarDateTime) { endDate = EndDate.Value; } else { endDate = context.CurrentCalendarEvents.FirstOrDefault(e => e.CalendarDateTime >= DateTime.Now && e.CalendarEventType == (int)CalendarEventType.LastDayOfSchool).CalendarDateTime; } while (startDate < endDate) { if (context.IsSchoolDay(startDate)) { retVal++; } startDate = startDate.AddDays(1); } //Store it if it's a normal year count if (!StartDate.HasValue && !EndDate.HasValue) { normalSchoolDayCount = retVal; } return(retVal); }
public static void EnsureSeedData(this DSMSDBContext context) { if (context.AllMigrationsApplied()) { if (!context.CalendarEvents.Any()) { context.CalendarEvents.Add(new CalendarEvent() { CalendarDateTime = DateTime.Parse("08/01/" + DateTime.Now.Year), CalendarEventType = (int)CalendarEventType.FirstDayOfSchool }); context.CalendarEvents.Add(new CalendarEvent() { CalendarDateTime = DateTime.Parse("05/01/" + (DateTime.Now.Year + 1)), CalendarEventType = (int)CalendarEventType.LastDayOfSchool }); } if (!context.SystemMessages.Any()) { context.SystemMessages.Add(new SystemMessage() { Title = "Test Message", Message = "This is a message for everyone" }); } SchoolMeetingMember smm = null; if (!context.SchoolMeetingMembers.Any()) { smm = new SchoolMeetingMember() { FirstName = "Gary", LastName = "GoodTwoShoes" }; context.SchoolMeetingMembers.Add(smm); } else { smm = context.SchoolMeetingMembers.First(); } if (!context.Visitors.Any()) { context.Visitors.Add(new Visitor() { Name = "Ran Dumb", ResponsibleParty = smm, ReasonForVisit = "Goofing Off", TimeIn = DateTime.Now, ExpectedDuration = 2.0 }); } context.SaveChanges(); } }