コード例 #1
0
        private static void 示範實體狀態用法(ContosoUniversityEntities db)
        {
            var one = db.Course.Find(14);

            db.Entry(one).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();

            var c = new Course()
            {
                CourseID = 14
            };

            db.Entry(c).State = System.Data.Entity.EntityState.Deleted;
            db.Course.Remove(c);
            db.SaveChanges();

            Console.WriteLine(db.Entry(one).State);
            one.Credits += 1;
            Console.WriteLine(db.Entry(one).State);

            //db.Entry(one).State = System.Data.Entity.EntityState.Deleted;
            db.SaveChanges();

            Console.WriteLine(db.Entry(one).State);
        }
コード例 #2
0
        private static void DeleteCourse(ContosoUniversityEntities db)
        {
            var course = db.Course.Find(9);

            db.Course.Remove(course);
            db.SaveChanges();
        }
コード例 #3
0
        private static void SaveChangesWithEntityStates(ContosoUniversityEntities db)
        {
            var c = new Course
            {
                Title      = "Entity Framework 6.1",
                Credits    = 10,
                Department = db.Department.Find(1)
            };

            db.Course.Add(c);
            db.SaveChanges();

            var id = c.CourseID;

            db.Entry(c).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();

            db.Entry(c).State = System.Data.Entity.EntityState.Detached;

            var d = new Course {
                CourseID = id
            };

            db.Entry(d).State = System.Data.Entity.EntityState.Deleted;
            db.SaveChanges();
        }
コード例 #4
0
        static void Main(string[] args)
        {
            using (var db = new ContosoUniversityEntities())
            {
                db.Database.Log = (sql) => { Console.WriteLine(sql); };

                //GetCourse_Git(db);
                //GetDepartment(db);
                //AddCourse(db);
                //UpdateCourse(db);
                //DeleteCourse(db);
                //示範實體狀態用法(db);

                //var c = db.Course.Find(1);
                //c.Credits += 1;
                //Console.ReadLine();
                //db.SaveChanges();
                //Console.ReadLine();

                var data = db.GetCourse("%Git%");

                foreach (var item in data)
                {
                    Console.WriteLine(item.Title);
                }
            }
        }
コード例 #5
0
        private static void ShowEntityStates(ContosoUniversityEntities db)
        {
            var one = db.Course.Find(11);

            Console.WriteLine(db.Entry(one).State);
            one.Credits += 10;
            Console.WriteLine(db.Entry(one).State);
            db.Course.Remove(one);
            Console.WriteLine(db.Entry(one).State);
            db.SaveChanges();
            Console.WriteLine(db.Entry(one).State);
        }
コード例 #6
0
        private static void AddCourse(ContosoUniversityEntities db)
        {
            var course = new Course
            {
                Title   = "Entity Framework 6",
                Credits = 100
            };

            course.Department = db.Department.Find(2);
            db.Course.Add(course);
            db.SaveChanges();
        }
コード例 #7
0
        private static void UpdateCourse(ContosoUniversityEntities db)
        {
            var courses = db.Course.Where(c => c.Title.Contains("Git"));

            foreach (var course in courses)
            {
                course.Credits  += 10;
                course.CreatedOn = DateTime.Now;
                course.UpdatedOn = DateTime.Now;
            }
            db.SaveChanges();
        }
コード例 #8
0
        private static void UpdateCourse(ContosoUniversityEntities db)
        {
            var items = db.Course.Where(p => p.Title.Contains("Git"));

            foreach (var item in items)
            {
                item.Credits  += 1;
                item.CreatedOn = DateTime.Now;
                item.UpdatedOn = DateTime.Now;
            }
            db.SaveChanges();
        }
コード例 #9
0
        private static void GetCourse_Git(ContosoUniversityEntities db)
        {
            //var data = db.Course.Where(p => p.Title.Contains("Git")).ToList();
            var data = (from p in db.Course
                        where p.Title.Contains("Git")
                        select p).ToList();

            foreach (var item in data)
            {
                Console.WriteLine(item.Title + "\t" + item.Department.Name);
            }
        }
コード例 #10
0
        private static void GetCourse_Git(ContosoUniversityEntities db)
        {
            //var data = db.Course.ToList();
            //var data = db.Course.Where(c => c.Title.Contains("Git")).ToList();
            var data = (from p in db.Course
                        where p.Title.Contains("Git")
                        select p).ToList();

            foreach (var course in data)
            {
                Console.WriteLine(course.Title);
            }
        }
コード例 #11
0
        private static void AddCourse(ContosoUniversityEntities db)
        {
            var c = new Course()
            {
                Title     = "Entity Framework 6.1",
                Credits   = 100,
                IsDeleted = false
            };

            c.Department = db.Department.Find(2);
            db.Course.Add(c);
            db.SaveChanges();
        }
コード例 #12
0
        private static void DeleteCourse(ContosoUniversityEntities db)
        {
            //var c = db.Course.Find(9);

            //var c = db.Course.FirstOrDefault(p => p.Title.StartsWith("Git"));
            //db.Course.Remove(c);

            var c = db.Course.Where(p => p.Title.StartsWith("Git"));

            db.Course.RemoveRange(c);

            db.SaveChanges();
        }
コード例 #13
0
        private static void GetDepartment(ContosoUniversityEntities db)
        {
            var data = (from p in db.Department.Include("Course") select p);

            foreach (var dept in data)
            {
                Console.WriteLine(dept.Name);
                foreach (var course in dept.Course)
                {
                    Console.WriteLine("\t" + course.Title);
                }
            }
        }
コード例 #14
0
        private static void GetDepartments(ContosoUniversityEntities db)
        {
            // var departments = db.Department;
            var departments = db.Department.Include("Course");

            foreach (var department in departments)
            {
                Console.WriteLine(department.Name);
                foreach (var course in department.Course)
                {
                    Console.WriteLine("\t" + course.Title);
                }
            }
        }
コード例 #15
0
        private static void GetDepartmentsWithCourses()
        {
            using (var db = new ContosoUniversityEntities())
            {
                db.Database.Log = Console.WriteLine;

                //var departments = db.Department;
                var departments = db.Department.Include("Course");
                foreach (var department in departments)
                {
                    Console.WriteLine(department.Name);
                    foreach (var course in department.Course)
                    {
                        Console.WriteLine("\t" + course.Title);
                    }
                }
            }
        }
コード例 #16
0
        static void Main(string[] args)
        {
            using (var db = new ContosoUniversityEntities())
            {
                db.Database.Log = Console.WriteLine;

                //GetCourse_Git(db);
                //GetDepartments(db);
                //GetDepartmentsWithCourses();

                //AddCourse(db);
                //UpdateCourse(db);
                //DeleteCourse(db);

                //ShowEntityStates(db);
                //SaveChangesWithEntityStates(db);

                var courses = db.GetCourse("%Git%");
                foreach (var course in courses)
                {
                    Console.WriteLine(course.Title);
                }
            }
        }