Beispiel #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);
        }
Beispiel #2
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();
        }
Beispiel #3
0
        private static void DeleteCourse(ContosoUniversityEntities db)
        {
            var course = db.Course.Find(9);

            db.Course.Remove(course);
            db.SaveChanges();
        }
Beispiel #4
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);
        }
Beispiel #5
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();
        }
Beispiel #6
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();
        }
Beispiel #7
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();
        }
Beispiel #8
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();
        }
Beispiel #9
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();
        }