Esempio n. 1
0
        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 Transaction_with_explicit_commit_on_a_local_transaction_can_be_used()
        {
            // Since this test actually mutates the database outside of a transaction it needs
            // to use a special context and ensure that the database is deleted and created
            // each time the test is run.
            using (var context = new SimpleModelContextForCommit())
            {
                context.Database.Delete();
                context.Database.Create();

                // Begin a local transaction
                var transaction = BeginLocalTransaction(context);

                var product = new Product { Name = "Fanta" };
                context.Products.Add(product);
                context.SaveChanges();

                // Commit local transaction
                transaction.Commit();
                CloseEntityConnection(context);
            }

            using (var context = new SimpleModelContextForCommit())
            {
                Assert.True(context.Products.Where(p => p.Name == "Fanta").Any());
            }
        }
        public void In_memory_DbSet_can_be_used_for_Attach()
        {
            var set = new InMemoryNonGenericDbSet<Product>();
            var product = new Product();

            Assert.Same(product, set.Attach(product));
            Assert.Same(product, set.ToList<Product>().Single());
        }
        public void In_memory_DbSet_can_be_used_for_Find()
        {
            var product = new Product { Id = 1 };
            var set = new InMemoryNonGenericDbSet<Product>(
                new[] { product, new Product { Id = 2 } }, (d, k) => d.Single(p => p.Id == (int)k[0]));

            Assert.Same(product, set.Find(1));
        }
        public void In_memory_DbSet_can_be_used_for_Add()
        {
            var set = new InMemoryDbSet<Product>();
            var product = new Product();

            Assert.Same(product, set.Add(product));
            Assert.Same(product, set.Single());
        }
        public void In_memory_DbSet_can_be_used_for_Remove()
        {
            var set = new InMemoryNonGenericDbSet<Product>();
            var product = new Product();

            set.Add(product);

            Assert.Same(product, set.Remove(product));
            Assert.Empty(set);
        }
        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());
                }
            }
        }
        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");
            }
        }
        private void SaveChanges_saves_Added_Modified_Deleted_entities_implementation(Func<DbContext, int> saveChanges)
        {
            using (var context = new SimpleModelContext())
            {
                // Modified
                var product1 = context.Products.Find(1);
                product1.Name = "Smarties";

                // Deleted
                var product2 = context.Products.Find(2);
                context.Products.Remove(product2);

                // Added
                var product3 = new Product() { Name = "Branston Pickle" };
                context.Products.Add(product3);

                // Validate state before Save
                Assert.Equal(3, GetStateEntries(context).Count());
                Assert.Equal(EntityState.Modified, GetStateEntry(context, product1).State);
                Assert.Equal(EntityState.Deleted, GetStateEntry(context, product2).State);
                Assert.Equal(EntityState.Added, GetStateEntry(context, product3).State);

                // Save
                int savedCount = saveChanges(context);
                Assert.Equal(3, savedCount);

                // Validate state after Save
                Assert.Equal(2, GetStateEntries(context).Count());
                Assert.Equal(EntityState.Unchanged, GetStateEntry(context, product1).State);
                Assert.Equal(EntityState.Unchanged, GetStateEntry(context, product3).State);

                using (var context2 = new SimpleModelContext())
                {
                    var product1s = context2.Products.Find(product1.Id);
                    var product2s = context2.Products.Find(product2.Id);
                    var product3s = context2.Products.Find(product3.Id);

                    Assert.NotNull(product1s);
                    Assert.Null(product2s);
                    Assert.NotNull(product3s);

                    Assert.Equal("Smarties", product1s.Name);
                    Assert.Equal("Branston Pickle", product3s.Name);

                    Assert.Equal(2, GetStateEntries(context).Count());
                    Assert.Equal(EntityState.Unchanged, GetStateEntry(context2, product1s).State);
                    Assert.Equal(EntityState.Unchanged, GetStateEntry(context2, product3s).State);
                }
            }
        }
        public void Scenario_Using_two_databases()
        {
            EnsureDatabaseInitialized(() => new LocalDbLoginsContext());
            EnsureDatabaseInitialized(() => new SimpleLocalDbModelContext());

            using (var context = new LocalDbLoginsContext())
            {
                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);

                Assert.Equal(@"(localdb)\v11.0", context.Database.Connection.DataSource);
            }

            using (var context = new SimpleLocalDbModelContext())
            {
                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));

                Assert.Equal(@"(localdb)\v11.0", context.Database.Connection.DataSource);
            }
        }
Esempio n. 11
0
        public void Scenario_Using_two_databases()
        {
            EnsureDatabaseInitialized(() => new LoginsContext());
            EnsureDatabaseInitialized(() => new SimpleModelContext());

            ExtendedSqlAzureExecutionStrategy.ExecuteNew(
                () =>
                {
                    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);
                        }
                    }
                });

            ExtendedSqlAzureExecutionStrategy.ExecuteNew(
                () =>
                {
                    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_query()
        {
            EnsureDatabaseInitialized(() => new SimpleModelContext());

            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));
                }
            }
        }
        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);
                }
            }
        }
Esempio n. 14
0
        private void SaveChanges_bubbles_UpdateException_implementation(Func<DbContext, int> saveChanges)
        {
            using (var context = new SimpleModelContext())
            {
                ExtendedSqlAzureExecutionStrategy.ExecuteNew(
                    () =>
                    {
                        using (context.Database.BeginTransaction())
                        {
                            var prod = new Product
                            {
                                Name = "Wallaby Sausages",
                                CategoryId = "AUSSIE FOODS"
                            };
                            context.Products.Add(prod);

                            Assert.Throws<DbUpdateException>(() => saveChanges(context)).ValidateMessage(
                                "Update_GeneralExecutionException");
                        }
                    });
            }
        }
Esempio n. 15
0
        private void SaveChanges_saves_Added_Modified_Deleted_entities_implementation(Func<DbContext, int> saveChanges)
        {
            ExtendedSqlAzureExecutionStrategy.ExecuteNew(
                () =>
                {
                    using (var context = new SimpleModelContext("SimpleModel_Added_Modified_Deleted"))
                    {
                        context.Database.Delete();
                        context.Database.Initialize(force: false);

                        // Modified
                        var product1 = context.Products.Find(1);
                        product1.Name = "Smarties";

                        // Deleted
                        var product2 = context.Products.Find(2);
                        context.Products.Remove(product2);

                        // Added
                        var product3 = new Product
                        {
                            Name = "Branston Pickle"
                        };
                        context.Products.Add(product3);

                        // Validate state before Save
                        Assert.Equal(3, GetStateEntries(context).Count());
                        Assert.Equal(EntityState.Modified, GetStateEntry(context, product1).State);
                        Assert.Equal(EntityState.Deleted, GetStateEntry(context, product2).State);
                        Assert.Equal(EntityState.Added, GetStateEntry(context, product3).State);

                        // Save
                        var savedCount = saveChanges(context);
                        Assert.Equal(3, savedCount);

                        // Validate state after Save
                        Assert.Equal(2, GetStateEntries(context).Count());
                        Assert.Equal(EntityState.Unchanged, GetStateEntry(context, product1).State);
                        Assert.Equal(EntityState.Unchanged, GetStateEntry(context, product3).State);

                        using (var context2 = new SimpleModelContext("SimpleModel_Added_Modified_Deleted"))
                        {
                            var product1s = context2.Products.Find(product1.Id);
                            var product2s = context2.Products.Find(product2.Id);
                            var product3s = context2.Products.Find(product3.Id);

                            Assert.NotNull(product1s);
                            Assert.Null(product2s);
                            Assert.NotNull(product3s);

                            Assert.Equal("Smarties", product1s.Name);
                            Assert.Equal("Branston Pickle", product3s.Name);

                            Assert.Equal(2, GetStateEntries(context).Count());
                            Assert.Equal(EntityState.Unchanged, GetStateEntry(context2, product1s).State);
                            Assert.Equal(EntityState.Unchanged, GetStateEntry(context2, product3s).State);
                        }
                    }
                });
        }
        public void Scenario_Insert()
        {
            EnsureDatabaseInitialized(() => new SimpleLocalDbModelContext());

            using (var context = new SimpleLocalDbModelContext())
            {
                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);

                Assert.Equal(@"(localdb)\v11.0", context.Database.Connection.DataSource);
            }
        }
        public void Scenario_Relate_using_FK()
        {
            EnsureDatabaseInitialized(() => new SimpleLocalDbModelContext());

            using (var context = new SimpleLocalDbModelContext())
            {
                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);

                Assert.Equal(@"(localdb)\v11.0", context.Database.Connection.DataSource);
            }
        }
        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());
            }
        }
Esempio n. 19
0
        public void Scenario_Relate_using_FK()
        {
            EnsureDatabaseInitialized(() => new SimpleModelContext());

            ExtendedSqlAzureExecutionStrategy.ExecuteNew(
                () =>
                {
                    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);
                        }
                    }
                });
        }