Ejemplo n.º 1
0
        public void TestManual_Cant_AttachOverlappingGraphsFromDifferentContexts()
        {
            // Read Bills with only Courses loaded

            ManualBillEntity bill0;

            using (var context = new ManualDbContext())
            {
                bill0 = context.Bills.Include(x => x.Courses).Single();
            }

            // Read first Meal with Courses loaded

            ManualMealEntity meal0;

            using (var context = new ManualDbContext())
            {
                meal0 = context.Meals.Include(x => x.Courses).First();
            }

            // Now try and attach the graphs

            using (var context = new ManualDbContext())
            {
                context.Set <ManualMealEntity>().Attach(meal0);

                // Overlapping graph should fail

                var threwException = false;

                try
                {
                    context.Set <ManualBillEntity>().Attach(bill0); // Throwing an exception is not helpful
                }
                catch (InvalidOperationException)
                {
                    threwException = true;
                }
                finally
                {
                    Assert.IsTrue(threwException, "EF should screw you over because it doesn't know what to do with the conflict (type and key match, but different object instance)");
                }
            }
        }
Ejemplo n.º 2
0
        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");
            }
        }