Reputation: 475
I have a WPF (with MahApps.Metro
and Caliburn.Micro
) project using Entity Framework 6.1.1 (Latest) and all of the latest MySQL
.
Whenever i run my project it opens up a new connection to the mysql server, but as i exit the application the connection remains open and goes to sleep. Entering a new debug session opens up a new connection whilst the other remains asleep.
How can i make sure the connection gets closed properly upon exiting my application so i don't end up hitting my connection limit?
Query method
public void RetrieveCourses()
{
// Query database
var context = new TAModelContainer();
var data = context.Courses.ToList<Course>();
foreach (Course course in data)
{
_courses.Add(course);
}
}
Upvotes: 0
Views: 635
Reputation: 838
Dispose the context when you are done with it. Not doing so can exhaust your Connection Pool and you may fail to get a connection.
public void RetrieveCourses()
{
// Query database
using(var context = new TAModelContainer())
{
var data = context.Courses.ToList<Course>();
foreach (Course course in data)
{
_courses.Add(course);
}
}
}
Upvotes: 3