-
Notifications
You must be signed in to change notification settings - Fork 216
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
Comments
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. |
Thanks for your clarification @roji So, what I did was - Create a connection It seems it works. Can you see any problem of doing that? |
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? |
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: current version: |
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). |
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,
The text was updated successfully, but these errors were encountered: