Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing connections explicitly in Entity Framework

Reading the microsoft documentation http://msdn.microsoft.com/en-us/library/bb738684.aspx I see they explicitly open and close the connection

using (EntityConnection conn = new EntityConnection("name=AdventureWorksEntities"))
{ 
    conn.Open();
    ...
    conn.Close();
}

why is this necessary?

like image 820
Carlo V. Dango Avatar asked Mar 12 '11 22:03

Carlo V. Dango


People also ask

Do we need to close connection in Entity Framework?

If we have multiple DbContexts with the same connection then the connection should be disposed of whenever the first closes the connection. Similarly things should happen for the mixed mode of ADO.Net and Entity Framework. DbContext always closes the connection when it is disposed of.

Does DbContext dispose close connection?

So if you have more than one DbContext with the same connection whichever context is disposed first will close the connection (similarly if you have mixed an existing ADO.NET connection with a DbContext, DbContext will always close the connection when it is disposed).

When should you close a connection to a database?

Typically, a connection to a database is closed when the task ends. For performance reasons, an application might leave the JDBC connection open for a subsequent user of the same JVM to use. If the JDBC connection is left open, the application must ensure that JDBC resources are not leaked over time.

How does Entity Framework manage connections?

Entity Framework manages a connection pool, which means that EF will reuse connections when possible and only create new ones when it needs to. Whether or not each call creates a new connection depends on many factors. So it's hard to say whether any given set of calls will or will not create new connections.


1 Answers

This isn't the "normal" way of using EF. EF usually manages the connection for you. However:

Managing Connections in Object Services (Entity Framework)

Object Services exposes the EntityConnection by means of the Connection property. This enables you to manage the connection and transactions or to supply your own EntityConnection. This is useful when you want to hold open a connection within a short-lived object context to improve performance or to explicitly control transactions. The same provider connection used by the Entity Framework can be shared with other parts of an application.

The following considerations apply when managing connections:

  • The object context will open the connection if it is not already open before an operation. If the object context opens the connection during an operation, it will always close the connection when the operation is complete.

  • If you manually open the connection, the object context will not close it. Calling Close or Dispose will close the connection.

  • If the object context creates the connection, the connection will always be disposed when the context is disposed.

  • In a long-running object context, you must ensure that the context is disposed when it is no longer required.

  • If you supply an open EntityConnection for the object context, you must ensure that it is disposed.

So in short, normally you don't manage the connection, but if you want to do it manually for the reasons stated above, you can. If you decide to open the connection manually, you need to close it manually (EF doesn't make any assumptions for you if you decide to go manual).

like image 124
Giovanni Galbo Avatar answered Oct 04 '22 06:10

Giovanni Galbo