public static Course GetCourseByID(int id) { using (var db = new SchoolEntities()) { return(db.Courses.FirstOrDefault(c => c.CourseID == id)); } }
//call stored procedure public static void AddPerson(string lastName, string firstName, DateTime?hireDate, DateTime?enrollmentDate, string discriminator) { using (var context = new SchoolEntities()) { var courses = context.InsertPerson(lastName, firstName, hireDate, enrollmentDate, discriminator); } }
public static void AddCourse(Course course) { using (var db = new SchoolEntities()) { db.Courses.Add(course); db.SaveChanges(); } }
public static void DeleteCourse(int id) { var course = GetCourseByID(id); using (var db = new SchoolEntities()) { db.Courses.Remove(course); db.SaveChanges(); } }
public static void UpdateCourse(Course course, Course updatedCourse) { using (var db = new SchoolEntities()) { course.Title = updatedCourse.Title; course.Credits = updatedCourse.Credits; course.DepartmentID = updatedCourse.DepartmentID; db.SaveChanges(); } }
//stored procedures with multiple result set public static void GetAllCourses() { using (var db = new SchoolEntities()) { var cmd = db.Database.Connection.CreateCommand(); cmd.CommandText = "[dbo].[GetAllOnlineAndOnsiteCourse]"; try { db.Database.Connection.Open(); var reader = cmd.ExecuteReader(); // First results set var onlineCourses = ((IObjectContextAdapter)db) .ObjectContext .Translate <OnlineCourse>(reader, "OnlineCourses", MergeOption.AppendOnly); foreach (var item in onlineCourses) { Console.WriteLine(item.URL); } // Second result set reader.NextResult(); var posts = ((IObjectContextAdapter)db) .ObjectContext .Translate <OnsiteCourse>(reader, "OnsiteCourses", MergeOption.AppendOnly); foreach (var item in posts) { Console.WriteLine(item.Location); } } finally { db.Database.Connection.Close(); } } }