示例#1
0
        public void Explicit_rollback_on_a_local_transaction_can_be_used_to_rollback_a_transaction()
        {
            EnsureDatabaseInitialized(() => new SimpleModelContext());

            using (var context = new SimpleModelContext())
            {
                ExtendedSqlAzureExecutionStrategy.ExecuteNew(
                    () =>
                {
                    // 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());
                });
            }
        }
示例#2
0
        public void Explicit_rollback_can_be_used_to_rollback_a_transaction()
        {
            EnsureDatabaseInitialized(() => new SimpleModelContext());

            ExtendedSqlAzureExecutionStrategy.ExecuteNew(
                () =>
            {
                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());
                    }
                }
            });
        }
示例#3
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);
                    }
                }
            });
        }
        public void Generic_DbEntityEntry_State_calls_DetectChanges_for_detached_unchanged_entities()
        {
            ExtendedSqlAzureExecutionStrategy.ExecuteNew(
                () =>
            {
                using (var context = new SimpleModelContext())
                {
                    using (new TransactionScope())
                    {
                        var entry = context.Entry(new Product());
                        context.Products.Add(entry.Entity);
                        context.SaveChanges();

                        // Ensure that the entity doesn't have a change tracking proxy
                        Assert.Equal(
                            entry.Entity.GetType(),
                            ObjectContext.GetObjectType(entry.Entity.GetType()));

                        // DetectChanges is called the first time the state is queried for a detached entity
                        Assert.Equal(EntityState.Unchanged, entry.State);

                        entry.Entity.Name = "foo";

                        Assert.Equal(EntityState.Unchanged, entry.State);
                    }
                }
            });
        }
示例#5
0
        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));
                }
            }
        }
示例#6
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));
                    }
                }
            });
        }
示例#7
0
        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);
                }
            }
        }
示例#8
0
        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 SetupContext(SimpleModelContext context)
        {
            context.Configuration.UseDatabaseNullSemantics = false;

            context.Categories.Add(new Category {
                Id = "Fruit"
            });

            context.Products.Add(new Product {
                Name = "Grapes", CategoryId = "Fruit"
            });
            context.Products.Add(new Product {
                Name = null, CategoryId = "Fruit"
            });
            context.Products.Add(new Product {
                Name = "Bananas", CategoryId = "Fruit"
            });

            context.SaveChanges();
        }
示例#10
0
        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);
                }
            }
        }
示例#11
0
        public void DbEntityEntry_State_calls_DetectChanges_for_detached_modified_entities()
        {
            using (var context = new SimpleModelContext())
            {
                using (new TransactionScope())
                {
                    var entry = context.Entry((object)new Product());
                    context.Products.Add((Product)entry.Entity);
                    context.SaveChanges();

                    // Ensure that the entity doesn't have a change tracking proxy
                    Assert.Equal(
                        entry.Entity.GetType(),
                        ObjectContext.GetObjectType(entry.Entity.GetType()));

                    ((Product)entry.Entity).Name = "foo";

                    // DetectChanges is called the first time the state is queried for a detached entity
                    Assert.Equal(EntityState.Modified, entry.State);
                }
            }
        }