-
Notifications
You must be signed in to change notification settings - Fork 604
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
ProfiledDbConnection's methods after Dispose shouldn't all throw NullReferenceException #345
Conversation
…ReferenceException After I call `MiniProfilerEF6.Initialize()` then my automatic migrations fail. This is because when EF6 `System.Data.Entity.Internal.RepositoryBase.CreateConnection` (https://github.com/aspnet/EntityFramework6/blob/6.2.0/src/EntityFramework/Internal/RepositoryBase.cs#L35) is called, it checks the connections's State which, if the connection has been Disposed, throws a NullReferenceException because `_connection` gets null-ed on Dispose. This isn't the right behaviour for a DbConnection. If you `Close` a `SqlConnection` for example, you can still call members such as `State` and they don't throw. I therefore propose the `_connection` is left as-is so that the wrapped methods still delegate to it without throwing `NullReferenceExceptions`.
Related: dotnet/ef6/issues/398 and #243. |
Some behavior has shifted here - what version of EF Core are you on? I'm not against the change per-se, but need to think about it. cc @jdaigle |
Not on EF Core, this is ye olde .NET Framework. Like I said, I think ProfiledDbConnection shouldn't change the behaviour of the DbConneciton it's wrapping. |
Sorry, Core was my bad, I meant which version of EF 6? |
6.2.0 |
Oh geez they still haven't updated NuGet? Poking that issue again. I don't agree with the fix here, since EF is the issue and it just happens to work in some cases (it's really depending on the GC not kicking in earlier in some others). Using a |
Surely ProfiledDbConnection's role is to invisibly wrap another connection "warts and all"? SqlConnection.State doesn't throw NRE after you've Disposed it. Whether it should or not is moot. |
Its role is to also be responsible. If we don't release the connection when we're told to, that's a leak. Disposal is there for a reason and it very much matters at scale. For example, this change would likely take Stack Overflow offline under load. We can't make that change - it's an EF issue of using a disposed thing...and needs to be fixed there. I am unwilling to break everyone else for an EF 6.2 issue, that's a far worse option. |
OK, I've created a workaround for EF 6.2 in the meantime:
|
After I call
MiniProfilerEF6.Initialize()
then my automatic migrations fail. This is because when EF6System.Data.Entity.Internal.RepositoryBase.CreateConnection
(https://github.com/aspnet/EntityFramework6/blob/6.2.0/src/EntityFramework/Internal/RepositoryBase.cs#L35) is called, it checks the connections's State which, if the connection has been Disposed, throws a NullReferenceException because_connection
gets null-ed on Dispose.This isn't the right behaviour for a DbConnection. If you
Close
aSqlConnection
for example, you can still call members such asState
and they don't throw. I therefore propose the_connection
is left as-is so that the wrapped methods still delegate to it without throwingNullReferenceExceptions
.