コード例 #1
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"
                    });

                    // 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();
        }
コード例 #2
0
        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();
        }