Skip to content

Conversation

@ShadyNagy
Copy link
Contributor

This PR adds https://github.com/ShadyNagy/EfCore.InMemory.Transactions to the EF Core extensions list.

What it does

Provides seamless transaction support for EF Core's InMemory provider by offering safe extension methods (BeginTransactionSafeAsync) and a NoOpDbContextTransaction class. This eliminates the common "transactions with isolation level are not supported" error when running integration tests with InMemory database.

Why it's useful

Developers using transactions with isolation levels in production code face exceptions when testing with InMemory provider. This package allows the same code to work in both production and test environments without conditional logic scattered throughout the codebase.

The problem

// Throws: "Transactions with isolation level Serializable are not supported"
await context.Database.BeginTransactionAsync(IsolationLevel.Serializable);

The solution

// Works with both real DB and InMemory
await context.Database.BeginTransactionSafeAsync(IsolationLevel.Serializable);

Package info

Hi @ErikEJ @AndriySvyryd
I appreciate your support and review.
Thanks!

@ShadyNagy ShadyNagy requested a review from ErikEJ January 28, 2026 14:41
Copy link
Contributor

@ErikEJ ErikEJ left a comment

Choose a reason for hiding this comment

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

LGTM

@roji
Copy link
Member

roji commented Jan 28, 2026

I honestly was not aware we were throwing for transactions with a non-default isolation level. I don't think we should - regardless of the isolation level, when using the InMemory provider the transaction is simply ignored, so I think it makes just as much sense to do that when an isolation level is specified or not.

Or am I missing/misunderstanding something?

@ShadyNagy
Copy link
Contributor Author

Hi @roji, thanks for the feedback!
Just to clarify, according to the official EF Core documentation:

"Note that by default, if a transaction is started, the in-memory provider will throw an exception since transactions aren't supported."

So transactions are not "simply ignored" by default - they throw. You need to explicitly configure:

.ConfigureWarnings(b => b.Ignore(InMemoryEventId.TransactionIgnoredWarning))

to make BeginTransaction() work silently.

However, even WITH that configuration, calling BeginTransaction(IsolationLevel.Serializable) throws a different exception: "Transactions with isolation level Serializable are not supported".

So there are actually two separate issues:

  1. BeginTransaction() → throws TransactionIgnoredWarning (can be suppressed via ConfigureWarnings)
  2. BeginTransaction(IsolationLevel.X) → throws a different exception that cannot be suppressed

My package addresses both cases with a unified approach, allowing the same transaction code to work in production and tests without modifications.

@ShadyNagy
Copy link
Contributor Author

@roji, based on your feedback I've submitted a PR to fix this in EF Core itself:
dotnet/efcore#37579
This adds the IsolationLevel overloads to InMemoryTransactionManager that delegate to the existing parameterless methods.

In the meantime, my package (EfCore.InMemory.Transactions) can help developers who need this fix today or are using older EF Core versions (8, 9, 10) that won't receive this update.

@roji roji merged commit dce6f91 into dotnet:main Jan 29, 2026
4 checks passed
@ShadyNagy ShadyNagy deleted the patch-3 branch January 29, 2026 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants