-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Document connection obtained from DatabaseFacade.GetDbConnection() should normally not be disposed #11415
Comments
@ststeiger GetDbConnection provides access to the DbConnection that EF is using internally. Application code should not dispose it. |
@ajcvickers Improve docs? |
@ajcvickers I created dotnet/EntityFramework.Docs#665, however it seems to me that the most direct way to improve the documentation of this API would be to add information to the API's XML docs. Thoughts? |
Why doesn't it just create a new connection ? Or maybe more to the point: While it's nice to have compile-time checking on SQL-field presence, I think there are a lot of cases where EF just doesn't quite cut it. There, I'd like to make due with Dapper, but I don't want to manage the abstraction/DbFactory and the connection string more than once, especially if EF already does it for me. |
@divega Agreed--suggest we move it to the backlog for post 2.1. |
@ststeiger The intention is that the EF context acts as essentially your unit-of-work. The connection is exposed so that you can integrate both EF calls and lower-level ADO.NET calls into that unit of work using the same connection. Using EF as a factory for connections outside of this is not something we have heard people asking for in the past. It's probably something that EF could do, but it's not clear to me that a) it adds significant value and b) that it is an appropriate responsibility for EF to have. However, we will discuss it in triage and decide appropriately. |
Triage: moving to the backlog to update API docs to indicate that the connection obtained in this way should not be disposed. With regard to making EF a factory for connections, we decided not to do this because:
|
@ajcvickers where's the doc and what's the difference when work with external connection ? |
@John0King Update the API docs means update the API documentation in the source code, which hasn't been done yet, hence this issue is still open. With regard to external connections, if you create the DbConnection and pass it to EF, then you must dispose of it after the context has been disposed. If EF creates the DbConnection object, then you must not dispose of it. |
@ajcvickers Does this mean it will be the same as we pass a connection string to the |
@John0King Yes. |
* Document connection obtained from DatabaseFacade.GetDbConnection() should normally not be disposed Fixes #11415 * Add XML docs referencing how to determine the default CommandTimeout Fixes #17503 * Clarify behavior for EnsureExists with an empty database Fixes #17563 * A hyperlink to the DbContext.Database.Migrate() method would be useful Fixes #17571 * Default values for maxRetryCount, maxRetryDelay, and errorNumbersToAdd Fixes #17574 * Document that modifying entity states while iterating over entries can result in "Collection was modified" exception Fixes #18389 * Update API doc links to correctly reference external dependencies Fixes #18580 * Make it clearer how to access EF.Functions Fixes #21424 * Improve API docs for TrackGraph Fixes #22529
* Document connection obtained from DatabaseFacade.GetDbConnection() should normally not be disposed Fixes #11415 * Add XML docs referencing how to determine the default CommandTimeout Fixes #17503 * Clarify behavior for EnsureExists with an empty database Fixes #17563 * A hyperlink to the DbContext.Database.Migrate() method would be useful Fixes #17571 * Default values for maxRetryCount, maxRetryDelay, and errorNumbersToAdd Fixes #17574 * Document that modifying entity states while iterating over entries can result in "Collection was modified" exception Fixes #18389 * Update API doc links to correctly reference external dependencies Fixes #18580 * Make it clearer how to access EF.Functions Fixes #21424 * Improve API docs for TrackGraph Fixes #22529
* Update API docs * Document connection obtained from DatabaseFacade.GetDbConnection() should normally not be disposed Fixes #11415 * Add XML docs referencing how to determine the default CommandTimeout Fixes #17503 * Clarify behavior for EnsureExists with an empty database Fixes #17563 * A hyperlink to the DbContext.Database.Migrate() method would be useful Fixes #17571 * Default values for maxRetryCount, maxRetryDelay, and errorNumbersToAdd Fixes #17574 * Document that modifying entity states while iterating over entries can result in "Collection was modified" exception Fixes #18389 * Update API doc links to correctly reference external dependencies Fixes #18580 * Make it clearer how to access EF.Functions Fixes #21424 * Improve API docs for TrackGraph Fixes #22529 * Review feedback * Update src/EFCore/EF.cs Co-authored-by: Shay Rojansky <roji@roji.org> Co-authored-by: Shay Rojansky <roji@roji.org>
I have a question:
I wrote an extension method to execute a stored procedure.
Now, I use DatabaseFacade.GetDbConnection() to get a db connection.
So far so good, everything is working.
Had a little trouble with getting the factory from the connection, but solved that.
When testing, the procedure ran fine, but I noticed I get a NULL-reference exception on queries afterwards.
I first thought it was due to my provider extensions, but then I noticed, as soon as you do
this happend.
Is that a bug in EF, or am I, for some odd reason, not supposed to dispose the connection ?
The text was updated successfully, but these errors were encountered: