locked
Entity Framework Database Connection Disposing & Memory Leaks RRS feed

  • Question

  • Is it required to dispose Entity Framework Connections or does it get automatically disposed?  If we declare the connection within using statement then it is guaranteed to be disposed. However, is it required to dispose the connection in below 2 cases :

     1) Connection is created as class object as below. Does It require to be disposed? If required should I use IDisposable then dispose the connection in Dispose() method?

    

    2) Below is section of code where Dispose is called at the end of the method. However, the statements between declaration and dispose might throw an exception. So, Dispose statement won't be reachable if any exception occurs. If I don't use using statement should I dispose the connection in finally block because that is guaranteed to be called even if exception occurs. 

    

    I was advised by one of my college that this is unnecessary as Entity Framework handles connections by it self and disposing the connections is unnecessary. So need an expert advise?

    Wednesday, June 4, 2014 2:59 PM

Answers

  • DbContext implmenets IDisposable, so you should dispose it.

    public class DbContext : IDisposable, IObjectContextAdapter

    http://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext(v=vs.113).aspx

    > 1) Connection is created as class object as below

    In this case the enclosing class should implement IDisposable and dispose the DbContext in its Dispose() implementation.

    > 2) [used directly]

    In this case create it in a Using block, or use a Finally to dispose it.

    >I was advised by one of my college that this is unnecessary as Entity Framework handles connections by it self and disposing the [DbContext] is unnecessary.

    This is not quite true.  DbContext will open and close the underlying SqlConnection as necessary, but it also maintains a large cache of tracked objects.  And if you explicitly open the underlying SqlConnection (which you often need to do for ADO.NET interop) then the DbContext.Dispose will actually close the SqlConnection.

    David


    David http://blogs.msdn.com/b/dbrowne/

    Wednesday, June 4, 2014 4:14 PM

All replies

  • DbContext implmenets IDisposable, so you should dispose it.

    public class DbContext : IDisposable, IObjectContextAdapter

    http://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext(v=vs.113).aspx

    > 1) Connection is created as class object as below

    In this case the enclosing class should implement IDisposable and dispose the DbContext in its Dispose() implementation.

    > 2) [used directly]

    In this case create it in a Using block, or use a Finally to dispose it.

    >I was advised by one of my college that this is unnecessary as Entity Framework handles connections by it self and disposing the [DbContext] is unnecessary.

    This is not quite true.  DbContext will open and close the underlying SqlConnection as necessary, but it also maintains a large cache of tracked objects.  And if you explicitly open the underlying SqlConnection (which you often need to do for ADO.NET interop) then the DbContext.Dispose will actually close the SqlConnection.

    David


    David http://blogs.msdn.com/b/dbrowne/

    Wednesday, June 4, 2014 4:14 PM
  • @ David 

           If  you r  using NInject framwork along repository pattern need   to call   dispose

          

    Wednesday, June 4, 2014 7:12 PM