public virtual void SaveChanges_uses_enlisted_transaction_after_ambient_transaction()
        {
            if (!AmbientTransactionsSupported)
            {
                return;
            }

            if (TestStore.ConnectionState == ConnectionState.Closed)
            {
                TestStore.OpenConnection();
            }

            using (var context = CreateContext())
            {
                using (TestUtilities.TestStore.CreateTransactionScope())
                {
                    context.Add(
                        new TransactionCustomer
                    {
                        Id   = 77,
                        Name = "Bobble"
                    });
                    context.Entry(context.Set <TransactionCustomer>().Last()).State = EntityState.Added;
                }

                using (var transaction = new CommittableTransaction(TimeSpan.FromMinutes(10)))
                {
                    context.Database.EnlistTransaction(transaction);

                    Assert.Throws <DbUpdateException>(() => context.SaveChanges());
                }
            }

            AssertStoreInitialState();
        }
        public virtual async Task SaveChanges_uses_ambient_transaction(bool async, bool closeConnection, bool autoTransactionsEnabled)
        {
            if (!AmbientTransactionsSupported)
            {
                return;
            }

            if (closeConnection)
            {
                TestStore.CloseConnection();
            }
            else if (TestStore.ConnectionState == ConnectionState.Closed)
            {
                TestStore.OpenConnection();
            }

            using (TestUtilities.TestStore.CreateTransactionScope())
            {
                using (var context = CreateContext())
                {
                    context.Database.AutoTransactionsEnabled = autoTransactionsEnabled;

                    context.Add(
                        new TransactionCustomer
                    {
                        Id   = 77,
                        Name = "Bobble"
                    });

                    // Issue #14935. Cannot eval 'Last()'
                    // Use AsEnumerable().
                    context.Entry(context.Set <TransactionCustomer>().AsEnumerable().Last()).State = EntityState.Added;

                    if (async)
                    {
                        await Assert.ThrowsAsync <DbUpdateException>(() => context.SaveChangesAsync());
                    }
                    else
                    {
                        Assert.Throws <DbUpdateException>(() => context.SaveChanges());
                    }

                    context.Database.AutoTransactionsEnabled = true;
                }

                Assert.Equal(
                    RelationalResources.LogAmbientTransactionEnlisted(new TestLogger <RelationalLoggingDefinitions>()).GenerateMessage("Serializable"),
                    Fixture.ListLoggerFactory.Log.First().Message);
            }

            if (closeConnection)
            {
                using (var context = CreateContext())
                {
                    context.Database.OpenConnection();
                }
            }

            AssertStoreInitialState();
        }
Example #3
0
        protected TransactionTestBase(TFixture fixture)
        {
            Fixture = fixture;
            Fixture.Reseed();

            if (TestStore.ConnectionState == ConnectionState.Closed)
            {
                TestStore.OpenConnection();
            }
        }
Example #4
0
        protected TransactionTestBase(TFixture fixture)
        {
            Fixture = fixture;
            Fixture.Reseed();

            if (TestStore.ConnectionState == ConnectionState.Closed)
            {
                TestStore.OpenConnection();
            }

            Fixture.ListLoggerFactory.Log.Clear();
        }
Example #5
0
        public virtual async Task SaveChanges_uses_ambient_transaction(bool async, bool closeConnection, bool autoTransactionsEnabled)
        {
            if (!AmbientTransactionsSupported)
            {
                return;
            }

            if (closeConnection)
            {
                TestStore.CloseConnection();
            }
            else if (TestStore.ConnectionState == ConnectionState.Closed)
            {
                TestStore.OpenConnection();
            }

            using (TestUtilities.TestStore.CreateTransactionScope())
            {
                using (var context = CreateContext())
                {
                    context.Database.AutoTransactionsEnabled = autoTransactionsEnabled;

                    context.Add(new TransactionCustomer {
                        Id = 77, Name = "Bobble"
                    });
                    context.Entry(context.Set <TransactionCustomer>().Last()).State = EntityState.Added;

                    if (async)
                    {
                        await Assert.ThrowsAsync <DbUpdateException>(() => context.SaveChangesAsync());
                    }
                    else
                    {
                        Assert.Throws <DbUpdateException>(() => context.SaveChanges());
                    }
                }

                Assert.Equal(RelationalStrings.LogAmbientTransactionEnlisted.GenerateMessage("Serializable"), Fixture.Log.Single().Message);

                if (closeConnection)
                {
                    // This could start a distributed transaction of the connection is reopened
                    using (var context = CreateContext())
                    {
                        Assert.NotNull(context.Set <TransactionCustomer>().Find(77));
                        context.Database.OpenConnection();
                    }
                }
            }

            AssertStoreInitialState();
        }
        public virtual void SaveChanges_allows_nested_ambient_transactions()
        {
            if (!AmbientTransactionsSupported)
            {
                return;
            }

            if (TestStore.ConnectionState == ConnectionState.Closed)
            {
                TestStore.OpenConnection();
            }

            using (var context = CreateContext())
            {
                using (var tr = new TransactionScope())
                {
                    context.Add(new TransactionCustomer {
                        Id = 77, Name = "Bobbie"
                    });
                    context.SaveChanges();
                    tr.Complete();
                    TestStore.CloseConnection();
                    using (var nestedTransaction = new TransactionScope(TransactionScopeOption.RequiresNew))
                    {
                        context.Add(new TransactionOrder {
                            Id = 300, Name = "Order3"
                        });
                        context.SaveChanges();
                        nestedTransaction.Complete();
                        TestStore.CloseConnection();
                    }
                }

                Assert.Equal(
                    new List <int>
                {
                    1,
                    2,
                    77
                },
                    context.Set <TransactionCustomer>().OrderBy(c => c.Id).Select(e => e.Id).ToList());
                Assert.Equal(
                    new List <int>
                {
                    100,
                    200,
                    300
                },
                    context.Set <TransactionOrder>().OrderBy(c => c.Id).Select(e => e.Id).ToList());
            }
        }
        public virtual async Task SaveChanges_uses_enlisted_transaction_after_connection_closed(bool async, bool autoTransactionsEnabled)
        {
            if (!AmbientTransactionsSupported)
            {
                return;
            }

            using (var context = CreateContext())
            {
                using (var transaction = new CommittableTransaction(TimeSpan.FromMinutes(10)))
                {
                    context.Database.EnlistTransaction(transaction);
                    context.Database.AutoTransactionsEnabled = autoTransactionsEnabled;

                    context.Add(
                        new TransactionCustomer
                    {
                        Id   = 77,
                        Name = "Bobble"
                    });

                    // Issue #14935. Cannot eval 'Last()'
                    // Use AsEnumerable().
                    context.Entry(context.Set <TransactionCustomer>().AsEnumerable().Last()).State = EntityState.Added;

                    context.Database.AutoTransactionsEnabled = true;
                }

                using (var transaction = new CommittableTransaction(TimeSpan.FromMinutes(10)))
                {
                    TestStore.CloseConnection();
                    TestStore.OpenConnection();
                    context.Database.EnlistTransaction(transaction);

                    if (async)
                    {
                        await Assert.ThrowsAsync <DbUpdateException>(() => context.SaveChangesAsync());
                    }
                    else
                    {
                        Assert.Throws <DbUpdateException>(() => context.SaveChanges());
                    }
                }
            }

            AssertStoreInitialState();
        }
        public virtual async Task SaveChanges_uses_ambient_transaction(bool async, bool autoTransactionsEnabled)
        {
            if (TestStore.ConnectionState == ConnectionState.Closed)
            {
                TestStore.OpenConnection();
            }

            using (TestUtilities.TestStore.CreateTransactionScope())
            {
                using (var context = CreateContext())
                {
                    context.Database.AutoTransactionsEnabled = autoTransactionsEnabled;

                    context.Add(
                        new TransactionCustomer {
                        Id = 77, Name = "Bobble"
                    });

                    context.Entry(context.Set <TransactionCustomer>().OrderBy(c => c.Id).Last()).State = EntityState.Added;

                    if (async)
                    {
                        await Assert.ThrowsAsync <DbUpdateException>(() => context.SaveChangesAsync());
                    }
                    else
                    {
                        Assert.Throws <DbUpdateException>(() => context.SaveChanges());
                    }

                    context.Database.AutoTransactionsEnabled = true;
                }

                if (AmbientTransactionsSupported)
                {
                    Assert.Equal(
                        RelationalResources.LogAmbientTransactionEnlisted(new TestLogger <TestRelationalLoggingDefinitions>())
                        .GenerateMessage("Serializable"),
                        Fixture.ListLoggerFactory.Log.Skip(2).First().Message);
                }
                else
                {
                    Assert.Equal(
                        RelationalResources.LogAmbientTransaction(new TestLogger <TestRelationalLoggingDefinitions>()).GenerateMessage(),
                        Fixture.ListLoggerFactory.Log.Skip(2).First().Message);

                    using (var context = CreateContext())
                    {
                        context.Entry(context.Set <TransactionCustomer>().Single(c => c.Id == 77)).State = EntityState.Deleted;

                        if (async)
                        {
                            await context.SaveChangesAsync();
                        }
                        else
                        {
                            context.SaveChanges();
                        }
                    }
                }
            }

            AssertStoreInitialState();
        }