public void QueryWithOverwriteChangesWillShowDeletedEntitiesAsDeleted()
        {
            // Create initial data in database
            int sueID = CustomerHelpers.AddCustomer("Sue", "VA");

            using (var context1 = new EFTestContext())
            {
                // Get Sue's customer entity, adding it to the cache
                var sue = context1.Customers.First(c => c.ID == sueID);
                // Name should be "Sue"
                sue.Name.Should().Be("Sue");

                // Delete data in other context (simulating a second user)
                using (var context2 = new EFTestContext())
                {
                    var sueToDelete = context2.Customers.First(c => c.ID == sueID);
                    context2.Customers.Remove(sueToDelete);
                    context2.SaveChanges();
                }

                // Verify the data has been deleted from the database
                using (var context3 = new EFTestContext())
                {
                    var sueIsDeleted = context3.Customers.FirstOrDefault(c => c.ID == sueID);
                    sueIsDeleted.Should().BeNull();
                }

                // Get Sue's customer entity using MergeOptions.OverwriteChanges, updating the entity in the cache
                var objectContext = ((IObjectContextAdapter)context1).ObjectContext;
                var objectQuery   = objectContext.CreateObjectSet <Customer>().Where(c => c.ID == sueID);
                // Set the MergeOptions to overwrite what is in the cache
                (objectQuery as ObjectQuery <Customer>).MergeOption = MergeOption.OverwriteChanges;
                var sueFromOriginalContext = objectQuery.FirstOrDefault();
                // Verify that sue does not exist
                sueFromOriginalContext.Should().BeNull();

                // Caution: the original sue object we created doesn't know it's been deleted
                context1.Entry(sue).State.Should().Be(EntityState.Unchanged);
                // Caution: The original object still exists
                sue.Should().NotBeNull();
                sue.Name.Should().Be("Sue");
            }
        }
        public void ReloadEntityWillForceEFToUpdateEntityInCache()
        {
            // Create initial data in database
            int sueID = CustomerHelpers.AddCustomer("Sue", "VA");

            using (var context1 = new EFTestContext())
            {
                // Get Sue's customer entity
                var sue = context1.Customers.Where(c => c.ID == sueID).First();
                // Name should be "Sue"
                sue.Name.Should().Be("Sue");

                // Update data in other context (simulating a second user)
                using (var context2 = new EFTestContext())
                {
                    context2.Customers.First(c => c.ID == sueID).Name = "Susan";
                    context2.SaveChanges();
                }

                // Verify the name has been updated in the database
                using (var context3 = new EFTestContext())
                {
                    var sueIsUpdated = context3.Customers.First(c => c.ID == sueID);
                    sueIsUpdated.Name.Should().Be("Susan");
                }

                // Verify our original context has the old data
                var sueNotUpdated = context1.Customers.First(c => c.ID == sueID);
                sueNotUpdated.Name.Should().Be("Sue");

                // Force EF to reload Sue's data
                context1.Entry(sue).Reload();

                // Retrieve the updated record from context1, which shows us
                // that the cache has been updated
                var sueUpdatedInOriginalContext = context1.Customers.First(c => c.ID == sueID);
                sueUpdatedInOriginalContext.Name.Should().Be("Susan");
            }

            // Cleanup
            CustomerHelpers.DeleteCustomer(sueID);
        }
Example #3
0
        public void GetDatabaseValuesWillUpdateTheCache()
        {
            // Create initial data in database
            int sueID = CustomerHelpers.AddCustomer("Sue", "VA");

            using (var context1 = new EFTestContext())
            {
                // Get Sue's customer entity
                var sue = context1.Customers.First(c => c.ID == sueID);
                // Name should be "Sue"
                sue.Name.Should().Be("Sue");

                // Update data in other context (simulating a second user)
                using (var context2 = new EFTestContext())
                {
                    context2.Customers.First(c => c.ID == sueID).Name = "Susan";
                    context2.SaveChanges();
                }

                // Verify the name has been updated in the database
                using (var context3 = new EFTestContext())
                {
                    var sueIsUpdated = context3.Customers.First(c => c.ID == sueID);
                    sueIsUpdated.Name.Should().Be("Susan");
                }

                // Verify we still have stale data in the cache
                var sueNotUpdated = context1.Customers.First(c => c.ID == sueID);
                sueNotUpdated.Name.Should().Be("Sue");

                // Use GetDatabaseValues to get a dictionary of the current db values (ignoring the cache)
                DbPropertyValues sueDbValues = context1.Entry(sueNotUpdated).GetDatabaseValues();
                sueDbValues["Name"].Should().Be("Susan");

                // Verify we still have stale data in the cache
                var sueStillNotUpdated = context1.Customers.First(c => c.ID == sueID);
                sueStillNotUpdated.Name.Should().Be("Sue");
            }

            // Cleanup
            CustomerHelpers.DeleteCustomer(sueID);
        }
Example #4
0
 public void Update(int id, T obj)
 {
     dbSet.Attach(obj);
     _context.Entry(obj).State = EntityState.Modified;
     _context.SaveChanges();
 }