Exemplo n.º 1
0
        private static void CRUDCourWithRelation1(ContosoUniversity190324Entities db)
        {
            // 顯示程式碼
            db.Database.Log = (msg) => Console.WriteLine(msg);

            Department dept = new Department
            {
                Name      = "TEST",
                Budget    = 100,
                StartDate = DateTime.Now,
                ModifyOn  = DateTime.Now
            };

            dept.Courses.Add(new Course
            {
                Title      = "Test 課程1",
                Credits    = 1,
                Department = dept
            });

            dept.Courses.Add(new Course
            {
                Title      = "Test 課程2",
                Credits    = 2,
                Department = dept
            });
            db.Departments.Add(dept);
            db.SaveChanges();

            Console.WriteLine("產生部門id: " + dept.DepartmentID);
            Console.WriteLine("產生部門名稱: " + dept.Name);

            foreach (var c in dept.Courses)
            {
                Console.WriteLine("CourseId: " + c.CourseID.ToString());
            }

            var data = db.Departments.Find(dept.DepartmentID);

            data.Name = "Adjust Test";
            db.SaveChanges();

            Console.WriteLine("部門名稱調整為: " + data.Name);

            //db.Departments.Remove(data);
            //db.SaveChanges();
            //Console.WriteLine("部門 " + data.Name + " 已刪除");
        }
Exemplo n.º 2
0
        private static void Entry(ContosoUniversity190324Entities db)
        {
            db.Database.Log = (msg) => Console.WriteLine(msg);

            //var dept = new Department()
            //{
            //    DepartmentID = 17,
            //    Name = "酷奇資訊",
            //    Budget = 18000,
            //    StartDate = new DateTime(2008, 1, 1, 0, 0, 0)
            //};

            //var entry = db.Entry(dept);

            // ---複製並修改
            var dept = db.Departments.Find(1);

            dept.Name = "修改後名稱";
            var entry = db.Entry(dept);

            entry.State = EntityState.Added;
            db.SaveChanges();

            Console.WriteLine(entry.State);
        }
Exemplo n.º 3
0
        private static void CCourseWithRelation(ContosoUniversity190324Entities db)
        {
            // 顯示程式碼
            db.Database.Log = (msg) => Console.WriteLine(msg);

            var course = db.Courses.Find(1);

            course.Instructors.Add(new Person()
            {
                FirstName     = "will",
                LastName      = "Huang",
                HireDate      = DateTime.Now,
                Discriminator = "XXX"
            });

            // enity錯誤處理
            try
            {
                db.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                var sb = new StringBuilder();
                foreach (var ex in e.EntityValidationErrors)
                {
                    foreach (var ve in ex.ValidationErrors)
                    {
                        sb.AppendLine($"欄位: {ve.PropertyName} 發生錯誤: {ve.ErrorMessage} ");
                    }
                }
                //Console.WriteLine(e.EntityValidationErrors);
                throw new Exception(sb.ToString(), e);
            }
        }
Exemplo n.º 4
0
        private static void ConcurrencyModel(ContosoUniversity190324Entities db)
        {
            db.Database.Log = (msg) => Console.WriteLine(msg);

            var dept = db.Departments.Find(1);

            dept.Budget++;

            Console.ReadLine();

            db.SaveChanges();
        }
Exemplo n.º 5
0
        private static void partialEntryEdit(ContosoUniversity190324Entities db)
        {
            db.Database.Log = (msg) => Console.WriteLine(msg);

            var dept = new Department()
            {
                Name      = "酷奇資訊",
                Budget    = 18000,
                StartDate = new DateTime(2008, 1, 1, 0, 0, 0)
            };

            db.Departments.Add(dept);
            db.SaveChanges();
        }
Exemplo n.º 6
0
        private static void Attach(ContosoUniversity190324Entities db)
        {
            var dept = new Department()
            {
                DepartmentID = 17,
                Name         = "酷奇資訊",
                Budget       = 18000,
                StartDate    = new DateTime(2008, 1, 1, 0, 0, 0)
            };

            db.Departments.Attach(dept);
            // 狀態變為unchanged

            //db.SaveChanges();
            // >> 這時候不會變更

            dept.Name = "123";
            db.SaveChanges();
        }