Поделиться через


Managing Connections in Object Services (Entity Framework)

By default, Object Services manages the connection to the database. It opens connections only when required, for example to execute a query or to call SaveChanges. Object Services then closes the connection when the operation is complete.

Calling any of the following methods opens the connection:

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.

The following example shows how to explicitly open a connection:

' Explicitly open the connection.
advWorksContext.Connection.Open()
// Explicitly open the connection.    
advWorksContext.Connection.Open();

For more information, see How to: Manually Open the Connection from the Object Context (Entity Framework).

When you manually open the connection in a long-running object context, you must call Dispose to ensure that the connection is closed when the context is no longer needed. You can also call Close on the EntityConnection to explicitly close the connection. For more information, see How to: Manage the Connection in a Long-Running Object Context (Entity Framework).

Object Services also enables you to create an EntityConnection and supply this connection to the object context. In this case, you can either open the connection manually or allow the object context to open it when needed. When you have supplied the EntityConnection to the object context, you must ensure that both the context and the EntityConnection are disposed when they are no longer required. The following example creates a connection and passes it to the object context:

' Create an EntityConnection.
Dim conn As New EntityConnection("name=AdventureWorksEntities")

' Create a long-running context with the connection.
Dim advWorksContext As New AdventureWorksEntities(conn)
// Create an EntityConnection.
EntityConnection conn =
    new EntityConnection("name=AdventureWorksEntities");

// Create a long-running context with the connection.
AdventureWorksEntities advWorksContext =
    new AdventureWorksEntities(conn);

For more information, see How to: Use EntityConnection with an Object Context (Entity Framework).

See Also

Other Resources

Managing the Object Context (Entity Framework)