public void Explicit_rollback_can_be_used_to_rollback_a_transaction() { EnsureDatabaseInitialized(() => new SimpleModelContext()); using (var tx = new TransactionScope()) { using (var context = new SimpleModelContext()) { var product = new Product { Name = "BestTea" }; context.Products.Add(product); context.SaveChanges(); Assert.Equal(1, GetTransactionCount(context.Database.Connection)); Assert.True(context.Products.Where(p => p.Name == "BestTea").AsNoTracking().Any()); // Rollback System Transaction tx.Dispose(); Assert.False(context.Products.Where(p => p.Name == "BestTea").AsNoTracking().Any()); } } }
public void Insert_In_SystemTransaction_without_commit_works_and_transaction_count_is_correct() { EnsureDatabaseInitialized(() => new SimpleModelContext()); using (new TransactionScope()) { using (var context = new SimpleModelContext()) { var product = new Product { Name = "Fanta" }; context.Products.Add(product); context.SaveChanges(); Assert.Equal(1, GetTransactionCount(context.Database.Connection)); Assert.True(context.Products.Where(p => p.Name == "Fanta").AsNoTracking().Any()); } } }
public void Explicit_rollback_on_a_local_transaction_can_be_used_to_rollback_a_transaction() { EnsureDatabaseInitialized(() => new SimpleModelContext()); using (var context = new SimpleModelContext()) { // Begin a local transaction var transaction = BeginLocalTransaction(context); var product = new Product() { Name = "New Tea" }; context.Products.Add(product); context.SaveChanges(); Assert.True(context.Products.Where(p => p.Name == "New Tea").AsNoTracking().Any()); // Rollback local transaction transaction.Rollback(); CloseEntityConnection(context); Assert.False(context.Products.Where(p => p.Name == "New Tea").AsNoTracking().Any()); } }
private void SaveChanges_bubbles_exception_during_AcceptChanges_implementation(Func<DbContext, int> saveChanges) { using (var context = new SimpleModelContext()) { var cat1 = new Category { Id = "AUSSIE FOODS" }; var cat2 = new Category { Id = "AUSSIE FOODS" }; context.Categories.Attach(cat1); context.Categories.Add(cat2); // Accept will fail because of PK violation // (cat1 doesn't actually exist in the store so update pipeline will succeed) Assert.Throws<InvalidOperationException>(() => context.SaveChanges()).ValidateMessage( "ObjectContext_AcceptAllChangesFailure"); } }
private void SaveChanges_bubbles_UpdateException_implementation(Func<DbContext, int> saveChanges) { using (var context = new SimpleModelContext()) { var prod = new Product() { Name = "Wallaby Sausages", CategoryId = "AUSSIE FOODS" }; context.Products.Add(prod); Assert.Throws<DbUpdateException>(() => context.SaveChanges()).ValidateMessage( "Update_GeneralExecutionException"); } }
public void SaveChanges_on_uninitialized_context_does_not_throw() { using (var context = new SimpleModelContext()) { Assert.Equal(0, context.SaveChanges()); } }
public void Scenario_Using_two_databases() { EnsureDatabaseInitialized(() => new LoginsContext()); EnsureDatabaseInitialized(() => new SimpleModelContext()); using (new TransactionScope()) { using (var context = new LoginsContext()) { var login = new Login() { Id = Guid.NewGuid(), Username = "******" }; context.Logins.Add(login); context.SaveChanges(); // Scenario ends; simple validation of final state follows Assert.Same(login, context.Logins.Find(login.Id)); Assert.Equal(EntityState.Unchanged, GetStateEntry(context, login).State); } } using (new TransactionScope()) { using (var context = new SimpleModelContext()) { var category = new Category() { Id = "Books" }; var product = new Product() { Name = "The Unbearable Lightness of Being", Category = category }; context.Products.Add(product); context.SaveChanges(); // Scenario ends; simple validation of final state follows Assert.Equal(EntityState.Unchanged, GetStateEntry(context, product).State); Assert.Equal(EntityState.Unchanged, GetStateEntry(context, category).State); Assert.Equal("Books", product.CategoryId); Assert.Same(category, product.Category); Assert.True(category.Products.Contains(product)); } } }
public void Scenario_Relate_using_FK() { EnsureDatabaseInitialized(() => new SimpleModelContext()); using (new TransactionScope()) { using (var context = new SimpleModelContext()) { var product = new Product() { Name = "Bovril", CategoryId = "Foods" }; context.Products.Add(product); context.SaveChanges(); // Scenario ends; simple validation of final state follows Assert.NotNull(product); Assert.Equal(EntityState.Unchanged, GetStateEntry(context, product).State); Assert.Equal("Foods", product.CategoryId); } } }
public void Scenario_Update() { EnsureDatabaseInitialized(() => new SimpleModelContext()); using (new TransactionScope()) { using (var context = new SimpleModelContext()) { var product = context.Products.Find(1); product.Name = "iSnack 2.0"; context.SaveChanges(); // Scenario ends; simple validation of final state follows Assert.Equal("iSnack 2.0", product.Name); Assert.Equal(EntityState.Unchanged, GetStateEntry(context, product).State); } } }
public void Scenario_Insert() { EnsureDatabaseInitialized(() => new SimpleModelContext()); using (new TransactionScope()) { using (var context = new SimpleModelContext()) { var product = new Product() { Name = "Vegemite" }; context.Products.Add(product); context.SaveChanges(); // Scenario ends; simple validation of final state follows Assert.NotEqual(0, product.Id); Assert.Equal(EntityState.Unchanged, GetStateEntry(context, product).State); } } }
public void Scenario_Relate_using_query() { EnsureDatabaseInitialized(() => new SimpleModelContext()); ExtendedSqlAzureExecutionStrategy.ExecuteNew( () => { using (new TransactionScope()) { using (var context = new SimpleModelContext()) { var category = context.Categories.Find("Foods"); var product = new Product { Name = "Bovril", Category = category }; context.Products.Add(product); context.SaveChanges(); // Scenario ends; simple validation of final state follows Assert.NotNull(product); Assert.Equal(EntityState.Unchanged, GetStateEntry(context, product).State); Assert.NotNull(category); Assert.Equal(EntityState.Unchanged, GetStateEntry(context, category).State); Assert.Equal("Foods", product.CategoryId); Assert.Same(category, product.Category); Assert.True(category.Products.Contains(product)); } } }); }