Вопрос

After I use

using (TestDatabaseEntitites ctx = new TestDatabaseEntitites())
{
    ....
}

I want all my sql connections to close after the using statement and I think that is what's expected. But it doesn't happen. Even after ctx.Dispose().

The only way I can close all the open connections is clearAllPools method. Am I doing something wrong? Thanks.

Это было полезно?

Решение

Connection pool connections in .Net stay open behind the scenes by design. The reason for this is that opening a connection to the database is generally a very expensive operation which would take literally seconds.

For this reason, closing a connection will only reset it, so that a future Open will simply be passed the old reset connection instead of opening a new one.

Другие советы

This answer is referred from here

DbContext is a very light-weight object.

It doesn't matter whether your DbContext stays alive or you instantiate it just before making the call because the actual DB Connection only opens when you SubmitChanges or Enumerate the query (in that case it is closed on end of enumeration).

In your specific case. It doesn't matter at all.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top