Do I need to force a Dispose after a LINQ query?

c#, database-connection, entity-framework, idisposable

Solution

By default DbContext automatically manages the connection for you. So you shouldn't have to explicitly call Dispose.

Blog post on the subject: Link

But I believe not disposing can cause performance issues if you're processing a lot of requests. You should add a using statement to see whether or not it's causing a problem in your case.

Problem

My DBA says that there are way too many connection open and he thinks it is my code in .net that is leaving them open. I am using LINQ querys and EF code first. Example Method: ``` public List<Stuff> GetStuff() { var db = new DBContext(); var results = db.stuff.toList(); return results; } ``` Do I need to dispose the db var once I am done? My understanding is that I didn't need to in EF and LINQ. Please point me to a Microsoft documentation about managing connection in code or best practices for LINQ/EF and db connections Update: I added ``` db.Connection.Close(); db.Dispose(); ``` and I still see the open connection in SQL after the two lines were executed. Is there a reason why it wouldn't close when I force it to close?

Original source