public void UsingANewContextWillNotShowDeletedData() { // Create initial data in database int sueID = CustomerHelpers.AddCustomer("Sue", "VA"); Customer originalSue; using (var context1 = new EFTestContext()) { // Get Sue's customer entity originalSue = context1.Customers.Where(c => c.ID == sueID).AsNoTracking().First(); // Name should be "Sue" originalSue.Name.Should().Be("Sue"); } // Delete data in other context (simulating a second user) using (var context2 = new EFTestContext()) { var existingSue = context2.Customers.First(c => c.ID == sueID); context2.Customers.Remove(existingSue); context2.SaveChanges(); } // Use a new context to retrieve the updated record using (var context3 = new EFTestContext()) { var sueIsDeleted = context3.Customers.FirstOrDefault(c => c.ID == sueID); sueIsDeleted.Should().BeNull(); } // Caution: the original entity you retrieved is still around, but not in the cache originalSue.Should().NotBeNull(); }
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); }
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); }
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); }
public void CacheKnowsWhenDataIsDeletedInDb() { // 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"); // Delete data using 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 record has been deleted from the database using (var context3 = new EFTestContext()) { var sueIsDeleted = context3.Customers.FirstOrDefault(c => c.ID == sueID); sueIsDeleted.Should().BeNull(); } // And our original context shows the record as deleted var sueNotDeleted = context1.Customers.FirstOrDefault(c => c.ID == sueID); sueNotDeleted.Should().BeNull(); } }
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); }
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 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); }
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); }
/// <summary> /// Reusable method to query data, unlike the example above changes are made in /// one place in code. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void IncludeStatementsUsingExtensionButton_Click(object sender, EventArgs e) { var customerIdentifier = ((CustomerLister)CustomerListBox.SelectedItem).Id;; using (var context = new NorthwindContext()) { context.Diagnostics = LogConsoleCheckBox.Checked; var customer = await context.CustomerPartial(customerIdentifier); FirstNameTextBox.Text = customer.Contact.FirstName; LastNameTextBox.Text = customer.Contact.LastName; ContactTypeTextBox.Text = customer.ContactTypeIdentifierNavigation.ContactTitle; CountryTextBox.Text = customer.CountryIdentifierNavigation.Name; } /* * Alternate to using an extension method as per above */ using (var context = new NorthwindContext()) { var customer = await CustomerHelpers.CustomerEntityAsync(context, customerIdentifier); } }
public async Task <IActionResult> StartCloud(ModelCustomer customer) { try { ServicesCeltaWare.Tools.CommandWin32.Copy(@"c:\Celta Business Solutions\Empty\", @"c:\Celta Business Solutions\" + customer.RootDirectory, true, true); var message = CustomerHelpers.CreateSite(customer); string messagePool = await CustomerHelpers.CreatePool(customer); string messageChangePool = await CustomerHelpers.ChangePool(customer, ServicesCeltaWare.Model.Enum.ProductName.None); customer.IsCloud = true; await _repository.UpdateAsynch(customer); return(Ok(message)); } catch (Exception err) { if (err.InnerException == null) { return(BadRequest(err.Message + "\n" + err.InnerException.Message)); } return(BadRequest(err.Message)); } }
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); } }