private static void PracticeCRUD(ContosoUniversityEntities db) { var dept = new Department() { Name = "酷奇資訊", Budget = 18000, StartDate = new DateTime(2019, 6, 1, 0, 0, 0), UpdatedOn = DateTime.Now }; dept.Course.Add(new Course() { Title = "ASP.NET MVC 5 開發實戰", Credits = Credits.VeryGood, Department = dept }); dept.Course.Add(new Course() { Title = "Angular 7 開發實戰", Credits = Credits.VeryGood, Department = dept }); Console.WriteLine("------ 新增部門與課程資料 ------"); db.Department.Add(dept); db.SaveChanges(); Console.WriteLine("------ 取得新增後的 DepartmentID ------"); Console.WriteLine("新部門 ID = " + dept.DepartmentID); Console.WriteLine("------ 重新查詢之前新增的部門資料 ------"); var new_dept = db.Department.Find(dept.DepartmentID); Console.WriteLine("部門名稱: " + new_dept.Name); Console.WriteLine("------ 查詢該部門所有課程資料 ------"); foreach (var course in new_dept.Course) { course.Credits = Credits.VeryGood; Console.WriteLine("\t" + course.Title + "\t" + course.Credits); } new_dept.UpdatedOn = DateTime.Now; Console.WriteLine("------ 更新該部門所有課程的 Credits 分數 ------"); db.SaveChanges(); db.Course.RemoveRange(new_dept.Course); db.Department.Remove(new_dept); Console.WriteLine("------ 移除課程資料與部門資料 ------"); db.SaveChanges(); }
private static void DeleteCourse(ContosoUniversityEntities db) { var c = db.Course.Find(9); db.Course.Remove(c); db.SaveChanges(); }
private static void ManyToManyAddWithExceptionHandling(ContosoUniversityEntities db) { var course = db.Course.Find(1); course.Instructors.Add(new Person() { FirstName = "Will", LastName = "Huang", HireDate = DateTime.Now //Discriminator = "" }); try { db.SaveChanges(); } catch (DbEntityValidationException ex) { StringBuilder sb = new StringBuilder(); foreach (var eve in ex.EntityValidationErrors) { foreach (var ve in eve.ValidationErrors) { sb.AppendLine($"欄位 {ve.PropertyName} 發生錯誤: {ve.ErrorMessage}"); } } throw new Exception(sb.ToString(), ex); } }
private static void UpdateDepartment(ContosoUniversityEntities db) { var dept = db.Department.Find(InsertedId); dept.Name = "John"; db.SaveChanges(); }
private static void RemoveDepartmentByState(ContosoUniversityEntities db) { db.Entry(new Department() { DepartmentID = 56 }).State = EntityState.Deleted; db.SaveChanges(); }
private static void UpdateCourse(ContosoUniversityEntities db) { var items = db.Course.Where(p => p.Title.Contains("Git")); foreach (var item in items) { item.MyCredits += 1; } db.SaveChanges(); // (執行此行程式,進行一次交易) 整批commit / rollback to db }
private static void DemoEntityEntryRemove(ContosoUniversityEntities db) { //var dept = db.Department.Find(22); //db.Department.Remove(dept); var dept = db.Department.Attach(new Department() { DepartmentID = 22 }); db.Entry(dept).State = EntityState.Deleted; db.SaveChanges(); }
private static void InsertDepartment(ContosoUniversityEntities db) { var dept = new Department() { Name = "Will", Budget = 100, StartDate = DateTime.Now }; db.Department.Add(dept); db.SaveChanges(); InsertedId = dept.DepartmentID; }
private static void AddCourse(ContosoUniversityEntities db) { var c = new Course() { Title = "Entity Framework 6.1", MyCredits = 100, CreatedOn = DateTime.Now, UpdatedOn = DateTime.Now }; c.Department = db.Department.Find(2); db.Course.Add(c); db.SaveChanges(); }
static void Main(string[] args) { using (var db = new ContosoUniversityEntities()) { db.Database.Log = Console.WriteLine; #region 延遲載入設定 //db.Configuration.LazyLoadingEnabled = false; // 關閉代理屬性 //db.Configuration.ProxyCreationEnabled = false; #endregion 延遲載入設定 db.Department.Add(new Department() { Budget = 100, Name = "TEST", StartDate = DateTime.Now.AddDays(+2) }); db.SaveChanges(); var department = db.Department.Include(c => c.Course).AsNoTracking(); foreach (var dept in department) { Console.WriteLine(dept.Name); foreach (var _cou in dept.Course) { Console.WriteLine(string.Format("\t{0}-{1}", _cou.CourseID, _cou.Title)); } } //QueryCourse(db); //InsertDepartment(db); //UpdateDepartment(db); //RemoveDepartment(db); Console.WriteLine("請輸入任意鍵繼續˙˙˙˙˙"); Console.ReadKey(); } }
private static void DemoAttachAndEntityState(ContosoUniversityEntities db) { var dept = new Department() { DepartmentID = 17, Name = "酷奇資訊2", Budget = 18000, StartDate = new DateTime(2019, 6, 1, 0, 0, 0), UpdatedOn = DateTime.Now }; db.Department.Attach(dept); dept.Name = "3"; db.SaveChanges(); }
static void Main(string[] args) { using (var db = new ContosoUniversityEntities()) { db.Database.Log = Console.WriteLine; //var department = db.Department.Include(x => x.Course); //foreach (var dept in department) //{ // Console.WriteLine(dept.Name); // foreach (var item in dept.Course) // { // Console.WriteLine("\t" + item.Title); // } //} var d = db.Department.Find(25); d.Name = "John" + DateTime.Now; Console.ReadLine(); db.SaveChanges(); //var d = db.Department.Find(1); //d.Name += "!"; //db.SaveChanges(); //QueryCourse(db); //InsertDepartment(db); //UpdateDepartment(db); //RemoveDepartment(db); } }
private static void RemoveDepartment(ContosoUniversityEntities db) { db.Department.Remove(db.Department.Find(InsertedId)); db.SaveChanges(); }
private static void Main(string[] args) { using (var db = new ContosoUniversityEntities()) { db.Database.Log = Console.WriteLine; var c = new Course() { Title = "Entity Framework 6.2", Credits = CourseCredits.Better, DepartmentID = 1 }; db.Course.Add(c); db.SaveChanges(); var item = db.Course.Find(c.CourseID); Console.WriteLine(item.Title + " " + item.Credits.ToString()); //db.Configuration.LazyLoadingEnabled = false; //db.Configuration.ProxyCreationEnabled = false; //var dept = db.Department.Find(1); //var dept = db.Course.Find(1); //dept.Title += "!"; //db.Entry(dept).State = EntityState.Added; //db.SaveChanges(); //var dept = db.Department.Find(20); //dept.Name = "John" + DateTime.Now; ////db.Entry(dept).State = EntityState.Added; //Console.ReadLine(); //db.SaveChanges(); //var department = db.Department.Include(p => p.Course); //foreach (var dept in department) //{ // Console.WriteLine(dept.Name); // foreach (var item in dept.Course) // { // Console.WriteLine("\t" + item.Title); // } //} //QueryCourse(db); //InsertDepartment(db); //UpdateDepartment(db); //RemoveDepartment(db); //var items = db.GetDepartment(); //foreach (var item in items) //{ // Console.WriteLine(item.Name); //} } }