Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            using (var db = new ContosoUniversityEntities())
            {
                // 印出sql指令
                db.Database.Log = (sql) => { Console.WriteLine(sql); };

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


                var one = db.Course.Find(6);
                //// 改狀態
                //db.Entry(one).State = System.Data.Entity.EntityState.Modified;
                //// 依狀態,所以執行update sql
                //db.SaveChanges();

                //var c = new Course() { CourseID = 7 };
                //// 改狀態
                //db.Entry(c).State = System.Data.Entity.EntityState.Deleted;
                //db.Course.Remove(c);
                //// 依狀態,所以執行delete sql
                //db.SaveChanges();

                // 這邊是看狀態的變化
                //Console.WriteLine(db.Entry(one).State);
                //one.MyCredits += 1;
                //Console.WriteLine(db.Entry(one).State);

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

                //Console.WriteLine(db.Entry(one).State);

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

                foreach (var item in data)
                {
                    Console.WriteLine(item.Title);
                }


                Console.ReadLine(); // 有這一行,console才不會馬上就被關掉
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            using (var db = new ContosoUniversityEntities())
            {
                db.Database.Log = (msg) =>
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(msg);
                    Console.ResetColor();
                };

                //SelectCourseByGitOrderByCredits(db);

                //SelectCourseWithRelation(db);
                //Console.WriteLine("--");
                //SelectDepartmentWithRelation(db);

                //PracticeCRUD(db);

                //PracticeLazyLoading(db);

                //ManyToManyAddWithExceptionHandling(db);

                //AsNoTracking(db);

                //DemoAttachAndEntityState(db);


                //var dept = new Department()
                //{
                //    DepartmentID = 17,
                //    Name = "酷奇資訊2",
                //    Budget = 18000,
                //    StartDate = new DateTime(2019, 6, 1, 0, 0, 0),
                //    UpdatedOn = DateTime.Now
                //};

                //DemoEntityEntryRemove(db);

                //var dept = db.Department.Add(new Department()
                //{
                //    Name = "酷奇資訊3",
                //    Budget = 18000,
                //    StartDate = new DateTime(2019, 6, 1, 0, 0, 0)
                //});
                //db.SaveChanges();

                //var dept = db.Department.Find(1);

                //dept.Budget += 1;

                //Console.ReadKey();

                //db.SaveChanges();

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

                foreach (var item in data)
                {
                    Console.WriteLine(item.Title);
                }
            }
        }