Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't throw when System.Transaction is not supported #17072

Merged
merged 1 commit into from
Aug 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions EFCore.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty?

7 changes: 7 additions & 0 deletions src/EFCore.Relational/Storage/RelationalConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ public virtual Transaction EnlistedTransaction
/// <param name="transaction"> The transaction to be used. </param>
public virtual void EnlistTransaction(Transaction transaction)
{
if (!SupportsAmbientTransactions)
{
Dependencies.TransactionLogger.AmbientTransactionWarning(this, DateTimeOffset.UtcNow);
return;
}

if (transaction != null)
{
Dependencies.TransactionLogger.ExplicitTransactionEnlisted(this, transaction);
Expand Down Expand Up @@ -563,6 +569,7 @@ private void HandleAmbientTransactions()
&& !SupportsAmbientTransactions)
{
Dependencies.TransactionLogger.AmbientTransactionWarning(this, DateTimeOffset.UtcNow);
return;
}

if (Equals(current, _ambientTransaction))
Expand Down
71 changes: 55 additions & 16 deletions test/EFCore.Relational.Specification.Tests/TransactionTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,6 @@ public virtual async Task SaveChangesAsync_implicitly_starts_transaction()
[InlineData(false, false)]
public virtual async Task SaveChanges_uses_enlisted_transaction(bool async, bool autoTransactionsEnabled)
{
if (!AmbientTransactionsSupported)
{
return;
}

using (var transaction = new CommittableTransaction(TimeSpan.FromMinutes(10)))
{
using (var context = CreateContext())
Expand Down Expand Up @@ -195,9 +190,35 @@ public virtual async Task SaveChanges_uses_enlisted_transaction(bool async, bool
context.Database.AutoTransactionsEnabled = true;
}

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

if (!autoTransactionsEnabled)
{
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();
Expand Down Expand Up @@ -308,11 +329,6 @@ public virtual async Task SaveChanges_uses_enlisted_transaction_connectionString
[InlineData(false, false)]
public virtual async Task SaveChanges_uses_ambient_transaction(bool async, bool autoTransactionsEnabled)
{
if (!AmbientTransactionsSupported)
{
return;
}

if (TestStore.ConnectionState == ConnectionState.Closed)
{
TestStore.OpenConnection();
Expand Down Expand Up @@ -345,9 +361,32 @@ public virtual async Task SaveChanges_uses_ambient_transaction(bool async, bool
context.Database.AutoTransactionsEnabled = true;
}

Assert.Equal(
RelationalResources.LogAmbientTransactionEnlisted(new TestLogger<TestRelationalLoggingDefinitions>()).GenerateMessage("Serializable"),
Fixture.ListLoggerFactory.Log.Skip(2).First().Message);
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();
Expand Down
8 changes: 4 additions & 4 deletions test/EFCore.Sqlite.FunctionalTests/TransactionSqliteTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public TransactionSqliteTest(TransactionSqliteFixture fixture)
protected override DbContext CreateContextWithConnectionString()
{
var options = Fixture.AddOptions(
new DbContextOptionsBuilder().UseSqlite(TestStore.ConnectionString))
new DbContextOptionsBuilder().UseSqlite(TestStore.ConnectionString)
.ConfigureWarnings(w => w.Log(RelationalEventId.AmbientTransactionWarning)))
.UseInternalServiceProvider(Fixture.ServiceProvider);

return new DbContext(options.Options);
Expand All @@ -40,9 +41,8 @@ public override void Reseed()
}

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
{
return base.AddOptions(builder);
}
=> base.AddOptions(builder)
.ConfigureWarnings(w => w.Log(RelationalEventId.AmbientTransactionWarning));
}
}
}