예제 #1
0
 public static Course GetCourseByID(int id)
 {
     using (var db = new SchoolEntities())
     {
         return(db.Courses.FirstOrDefault(c => c.CourseID == id));
     }
 }
예제 #2
0
        //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);
            }
        }
예제 #3
0
 public static void AddCourse(Course course)
 {
     using (var db = new SchoolEntities())
     {
         db.Courses.Add(course);
         db.SaveChanges();
     }
 }
예제 #4
0
        public static void DeleteCourse(int id)
        {
            var course = GetCourseByID(id);

            using (var db = new SchoolEntities())
            {
                db.Courses.Remove(course);
                db.SaveChanges();
            }
        }
예제 #5
0
 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();
     }
 }
예제 #6
0
        //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();
                }
            }
        }