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

Share connection/transaction between two DbContext (question) #130

Closed
mayconbeserra opened this issue Dec 7, 2016 · 5 comments
Closed

Comments

@mayconbeserra
Copy link

Hi there,

This is a question. How could I share my connection/transaction between two different DbContexts?

I haven't seen options for that.

Regards,

@roji
Copy link
Member

roji commented Dec 7, 2016

You can't share the same connection across two open DbContexts - each DbContext is tied to a specific connection, and you can't use the same connection concurrently. However, Npgsql (and other database providers) provide connection pooling, which means that if you use two DbContexts one after the other (not at the same time), the second will likely reuse the connection opened for the first, to avoid the overhead of opening a new physical connection.

By sharing a transaction I assume you're talking about distributed transactions - the same transaction used across multiple connections, even multiple databases. This feature relies on ambient transactions, which (if I understand things right) isn't currently supported by EF Core: dotnet/efcore#5595

Am closing the issue, but let me know if anything is still unclear.

@roji roji closed this as completed Dec 7, 2016
@roji roji added the invalid label Dec 7, 2016
@mayconbeserra
Copy link
Author

Thanks for your clarification @roji

So, what I did was -

Create a connection
Create a DbContext1 with that connection
Open the transaction
Create a DbContext2 with the same connection
Attach the transaction for the dbContext2 (e.g. Db.Database.UseTransaction(transaction))

It seems it works.

Can you see any problem of doing that?

@roji
Copy link
Member

roji commented Dec 8, 2016

Well, I'm not knowledgeable enough about what kind of state DbContexts keep in order to know if this could cause an issue. At the very least you have to be very careful not to use the two contexts at the same time, which raises the question of what value this arrangement has - why not have one context and share that?

@marcwittke
Copy link

Being late to the party, but while asking the same question I Found out that this is a well documented feature since EF Core 1.0.0:
https://ef.readthedocs.io/en/staging/saving/transactions.html#cross-context-transaction-relational-databases-only

current version:
https://docs.microsoft.com/en-us/ef/core/saving/transactions#cross-context-transaction-relational-databases-only

@roji
Copy link
Member

roji commented Jan 29, 2019

Thanks for finding that out, two years ago I think I wasn't aware of this feature...

Note also dotnet/efcore#8494, which is about changing the connection of an existing DbContext (not supported at this time).

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

No branches or pull requests

3 participants