private static OnlineCourse GetOnlineCourse(string courseName) { using (var ctx = new CoreModelContext()) { return ctx.OnlineCourses.FirstOrDefault(c => c.Title == courseName); } }
private static OnlineCourse GetOnlineCourse(string courseName) { using (var ctx = new CoreModelContext()) { return(ctx.OnlineCourses.FirstOrDefault(c => c.Title == courseName)); } }
private static void UpdateCourse(Course course) { using (var ctx = new CoreModelContext()) { ctx.Entry(course).State = course.Id == 0 ? EntityState.Added : EntityState.Modified; ctx.SaveChanges(); } }
private static void DeleteCourse() { using (var ctx = new CoreModelContext()) { var course = ctx.Courses.FirstOrDefault(c => c.Title == "Market Efficiency"); ctx.Courses.Remove(course); ctx.SaveChanges(); } }
private static void GetCourses() { using (var ctx = new CoreModelContext()) { Console.WriteLine("In Get Courses"); var courses = ctx.OnlineCourses.ToList(); foreach (var onlineCourse in courses) { Console.WriteLine("Course " + onlineCourse.Id); } } }
private static void InsertInstructorParking() { using (var ctx = new CoreModelContext()) { var instructor = ctx.Instructors.First(i => i.LastName == "Li"); instructor.AssignedParking = new AssignedParking() { SpaceNumber = "PS 1" }; ctx.SaveChanges(); } }
private static void GetInstructor() { using (var ctx = new CoreModelContext()) { var query = ctx.Instructors.FirstOrDefault(); if (query != null) { var msg = string.Format("Instructor {0} : {1} {2} - Office at {3}", query.Id.ToString(), query.FirstName, query.LastName, query.Office.Location); Console.WriteLine(msg); } } }
private static void UpdateOnlineCourseUsingSetValue(Course course) { using (var ctx = new CoreModelContext()) { //var entry = ctx.Courses.Where(c => c.Id == course.Id).FirstOrDefault(); ctx.OnlineCourses.Load(); var entry = ctx.ChangeTracker.Entries <OnlineCourse>().FirstOrDefault(e => ((int)e.CurrentValues["Id"]) == course.Id); if (entry != null) { entry.CurrentValues.SetValues(course); //Great way to use DTO objects to set only subset of properties without empty out other property values ctx.SaveChanges(); } } }
private static void InsertDepartment() { using (var ctx = new CoreModelContext()) { var department = new Department { Name = "Economy", Budget = 200000m, StartDate = System.DateTime.Now, Courses = new List <Course> { new OnlineCourse() { Title = "Micro Economy", Credits = 3, Url = "http://www.microeconomy.com", Instructors = new List <Instructor>() { new Instructor() { FirstName = "Daniel", LastName = "Li", Gender = EGender.Male, Office = new Office() { Location = "Building 5" } } } } } }; if (ctx.Departments.Any(d => d.Name == "Economy") == false) { Console.WriteLine("Insert new Department"); ctx.Departments.Add(department); ctx.SaveChanges(); Console.WriteLine("End of Insert new Department"); } } }
private static void InsertCourse() { using (var ctx = new CoreModelContext()) { var courseTitle = "Market Efficiency"; var course = new OnSiteCourse() { Title = courseTitle, Credits = 2, Department = ctx.Departments.First(d => d.Name == "Economy"), Instructors = new List <Instructor>() { //new Instructor() //{ // FirstName = "Jennifer", // LastName = "Lopez", // Gender = EGender.Female, // Office = new Office() {Location = "Building 1"} //}, ctx.Instructors.First(i => i.LastName == "Lopez"), ctx.Instructors.First(i => i.LastName == "Li") }, Details = new Details() { DurationDays = 90, Location = "Building 5", Time = System.DateTime.Now } }; if (ctx.Courses.Any(c => c.Title == courseTitle) == false) { Console.WriteLine("Insert new course " + courseTitle); ctx.Courses.Add(course); ctx.SaveChanges(); Console.WriteLine("Finish inserting new course " + courseTitle); } } }
private static void InsertDepartment() { using (var ctx = new CoreModelContext()) { var department = new Department { Name = "Economy", Budget = 200000m, StartDate = System.DateTime.Now, Courses = new List<Course> { new OnlineCourse() { Title = "Micro Economy", Credits = 3, Url = "http://www.microeconomy.com", Instructors = new List<Instructor>() { new Instructor() { FirstName = "Daniel", LastName = "Li", Gender = EGender.Male, Office = new Office() {Location = "Building 5"} } } } } }; if (ctx.Departments.Any(d => d.Name == "Economy") == false) { Console.WriteLine("Insert new Department"); ctx.Departments.Add(department); ctx.SaveChanges(); Console.WriteLine("End of Insert new Department"); } } }
private static void UpdateOnlineCourseUsingSetValue(Course course) { using (var ctx = new CoreModelContext()) { //var entry = ctx.Courses.Where(c => c.Id == course.Id).FirstOrDefault(); ctx.OnlineCourses.Load(); var entry = ctx.ChangeTracker.Entries<OnlineCourse>().FirstOrDefault(e => ((int)e.CurrentValues["Id"]) == course.Id); if (entry != null) { entry.CurrentValues.SetValues(course); //Great way to use DTO objects to set only subset of properties without empty out other property values ctx.SaveChanges(); } } }
private static void InsertCourse() { using (var ctx = new CoreModelContext()) { var courseTitle = "Market Efficiency"; var course = new OnSiteCourse() { Title = courseTitle, Credits = 2, Department = ctx.Departments.First(d => d.Name == "Economy"), Instructors = new List<Instructor>() { //new Instructor() //{ // FirstName = "Jennifer", // LastName = "Lopez", // Gender = EGender.Female, // Office = new Office() {Location = "Building 1"} //}, ctx.Instructors.First(i => i.LastName == "Lopez"), ctx.Instructors.First(i => i.LastName == "Li") }, Details = new Details() { DurationDays = 90, Location = "Building 5", Time = System.DateTime.Now } }; if (ctx.Courses.Any(c => c.Title == courseTitle) == false) { Console.WriteLine("Insert new course " + courseTitle); ctx.Courses.Add(course); ctx.SaveChanges(); Console.WriteLine("Finish inserting new course " + courseTitle); } } }