Esempio n. 1
0
        public void AsNoTrackingDoesntReturnDeletedData()
        {
            // 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).AsNoTracking().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");
                }

                // But our original context has the stale data cached
                var sueStillUpdated = context1.Customers.First(c => c.ID == sueID);
                sueStillUpdated.Name.Should().Be("Susan");
            }

            // Cleanup
            CustomerHelpers.DeleteCustomer(sueID);
        }
Esempio n. 2
0
        public void UsingANewContextWillRetrieveUpdatedData()
        {
            // 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).AsNoTracking().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();
            }

            // Use a new context to retrieve the updated record
            using (var context3 = new EFTestContext())
            {
                var sueIsUpdated = context3.Customers.First(c => c.ID == sueID);
                sueIsUpdated.Name.Should().Be("Susan");
            }

            // Cleanup
            CustomerHelpers.DeleteCustomer(sueID);
        }
Esempio n. 3
0
        public void AsNoTrackingDoesntAddDataToCache()
        {
            // 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).AsNoTracking().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");
                }

                // We can retrieve the updated record from context1, which tells us that the
                // cache on context1 is empty (before we retrieved the data
                var sueStillUpdated = context1.Customers.First(c => c.ID == sueID);
                sueStillUpdated.Name.Should().Be("Susan");
            }

            // Cleanup
            CustomerHelpers.DeleteCustomer(sueID);
        }
Esempio n. 4
0
        public void CacheKnowsWhenDataIsAddedToDb()
        {
            // Create initial data in database
            int sueID = CustomerHelpers.AddCustomer("Sue", "VA");

            using (var context1 = new EFTestContext())
            {
                // Get count of customers in Virginia
                var numInVABefore = context1.Customers.Where(c => c.State == "VA").ToList().Count;

                // Add new customer using separate context (simulating a second user)
                int jamesID = CustomerHelpers.AddCustomer("James", "VA");

                // Verify new record exists in database using another context
                using (var context3 = new EFTestContext())
                {
                    var jamesExists = context3.Customers.FirstOrDefault(c => c.ID == jamesID);
                    jamesExists.Should().NotBeNull();
                }

                // Our original context shows the new record when queried
                var numInVAAfter = context1.Customers.Where(c => c.State == "VA").ToList().Count;
                numInVAAfter.Should().Be(numInVABefore + 1);
            }

            // Cleanup
            CustomerHelpers.DeleteCustomer(sueID);
        }
        public void QueryWithOverwriteChangesWillUpdateCacheFromDatabase()
        {
            // 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");

                // 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");
                }

                // 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.First();
                // Verify we retrieved the updated data
                sueFromOriginalContext.Name.Should().Be("Susan");

                // Get Sue from the cache again, verifying that the cache has been updated, this tme
                // querying without the OverwriteChanges MergeOption
                var sueFromOriginalContextWithoutOverwriteChanges = context1.Customers.First(c => c.ID == sueID);
                sueFromOriginalContextWithoutOverwriteChanges.Name.Should().Be("Susan");
            }

            // Cleanup
            CustomerHelpers.DeleteCustomer(sueID);
        }
Esempio n. 6
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);
        }
Esempio n. 7
0
        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);
        }
        public void StandardQueryWillNotOverwriteCacheAfterQueryWithOverwriteChanges()
        {
            // Create initial data in database
            int sueID = CustomerHelpers.AddCustomer("Sue", "VA");

            using (var context1 = new EFTestContext())
            {
                // Get Sue's customer entity using MergeOptions.OverwriteChanges, placing 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 sueNowInCache = objectQuery.First();
                // Verify we retrieved the original data
                sueNowInCache.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");
                }

                // Get Sue from the cache without the OverwriteChanges MergeOption
                var sueFromOriginalContext = context1.Customers.First(c => c.ID == sueID);
                // We should see the original data, because the cache hasn't been updated
                sueFromOriginalContext.Name.Should().Be("Sue");
            }

            // Cleanup
            CustomerHelpers.DeleteCustomer(sueID);
        }
Esempio n. 9
0
        public void RefreshingMultipleEntitiesInEFWillForceEFToUpdateEntityInCache()
        {
            try
            {
                // Create initial data in database
                int sueID = CustomerHelpers.AddCustomer("Sue", "VA");
                int jimID = CustomerHelpers.AddCustomer("Jim", "VA");

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

                    // Update data in other context (simulating a second user)
                    using (var context2 = new EFTestContext())
                    {
                        context2.Customers.First(c => c.ID == sueID).Name = "Susan";
                        context2.Customers.First(c => c.ID == jimID).Name = "James";
                        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");
                        var jimIsUpdated = context3.Customers.First(c => c.ID == jimID);
                        jimIsUpdated.Name.Should().Be("James");
                    }

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

                    // Force EF to refresh Sue's and Jim's data
                    var objectContext    = ((IObjectContextAdapter)context1).ObjectContext;
                    var objectsToRefresh = new Customer[] { jim, sue };
                    objectContext.Refresh(RefreshMode.StoreWins, objectsToRefresh);

                    // You can also query the ObjectStateManager for the objects you need to refresh
                    //var objectsToRefresh = objectContext.ObjectStateManager.GetObjectStateEntries(
                    //	EntityState.Added | EntityState.Deleted | EntityState.Modified | EntityState.Unchanged)
                    //	.Where(e => e.EntityKey != null)
                    //	.Select(e => e.Entity)
                    //	.OfType<Customer>()
                    //	.Where(c => (new int[] {sueID, jimID}).Contains(c.ID));
                    //objectContext.Refresh(RefreshMode.StoreWins, objectsToRefresh);

                    // Verify our original context has the new data
                    var sueUpdatedInOriginalContext = context1.Customers.First(c => c.ID == sueID);
                    sueUpdatedInOriginalContext.Name.Should().Be("Susan");
                    var jimUpdatedInOriginalContext = context1.Customers.First(c => c.ID == jimID);
                    jimUpdatedInOriginalContext.Name.Should().Be("James");
                }

                // Cleanup
                CustomerHelpers.DeleteCustomer(sueID);
                CustomerHelpers.DeleteCustomer(jimID);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }