public void TestManual_Can_AttachFullRootAndTouchTheChildren() { // Test we can attach Bill and touch all his children var bill = EntityMapper.ToManualBill(Values.Bill); using (var context = new ManualDbContext()) { // Attach object graph context.Set <ManualBillEntity>().Attach(bill); // Touch them AND make them dirty bill.LastUpdated = DateTime.Now; context.Entry(bill).State = EntityState.Modified; foreach (var foodCourse in bill.Courses) { foodCourse.LastUpdated = DateTime.Now; context.Entry(foodCourse).State = EntityState.Modified; } foreach (var meal in bill.Meals) { meal.LastUpdated = DateTime.Now; context.Entry(meal).State = EntityState.Modified; } // Save var rowsAffected = context.SaveChanges(); // All 6 entities should be modified Assert.AreEqual(6, rowsAffected, "Rows affected mismatch"); } }
public void TestManual_Cant_ReadNavigationPropertyOutsideContextAfterDetach() { // Read Bill with only Courses loaded and detach ManualBillEntity bill0; using (var context = new ManualDbContext()) { bill0 = context.Bills.Include(x => x.Courses).Single(); Assert.AreEqual(Values.CourseCount, bill0.Courses.Count, "Bill should have courses loaded"); context.Entry(bill0).State = EntityState.Detached; } Assert.IsNull(bill0.Meals, "Bill should not have a Meals collection"); // Not loaded, null is a good way of knowing it isn't loaded Assert.IsNotNull(bill0.Courses, "Bill should have a Courses collection"); Assert.AreEqual(0, bill0.Courses.Count, "Bill should have no Courses"); // Not helpful, the detach is a pain }