This topic has not yet been rated - Rate this topic

EntityConnection Class

Contains a reference to a conceptual model and a data source connection. This class cannot be inherited.

System.Object
  System.MarshalByRefObject
    System.ComponentModel.Component
      System.Data.Common.DbConnection
        System.Data.EntityClient.EntityConnection

Namespace:  System.Data.EntityClient
Assembly:  System.Data.Entity (in System.Data.Entity.dll)
public sealed class EntityConnection : DbConnection

The EntityConnection type exposes the following members.

  Name Description
Public method EntityConnection() Initializes a new instance of the EntityConnection class.
Public method EntityConnection(String) Initializes a new instance of the EntityConnection class, based on the connection string.
Public method EntityConnection(MetadataWorkspace, DbConnection) Initializes a new instance of the EntityConnection class with a specified MetadataWorkspace and DbConnection.
Top
  Name Description
Protected property CanRaiseEvents Gets a value indicating whether the component can raise an event. (Inherited from Component.)
Public property ConnectionString Gets or sets the EntityConnection connection string. (Overrides DbConnection.ConnectionString.)
Public property ConnectionTimeout Gets the number of seconds to wait when attempting to establish a connection before ending the attempt and generating an error. (Overrides DbConnection.ConnectionTimeout.)
Public property Container Gets the IContainer that contains the Component. (Inherited from Component.)
Public property Database Gets the name of the current database, or the database that will be used after a connection is opened. (Overrides DbConnection.Database.)
Public property DataSource Gets the name or network address of the data source to connect to. (Overrides DbConnection.DataSource.)
Protected property DbProviderFactory Gets the DbProviderFactory for this DbConnection. (Inherited from DbConnection.)
Protected property DesignMode Gets a value that indicates whether the Component is currently in design mode. (Inherited from Component.)
Protected property Events Gets the list of event handlers that are attached to this Component. (Inherited from Component.)
Public property ServerVersion Gets a string that contains the version of the data source to which the client is connected. (Overrides DbConnection.ServerVersion.)
Public property Site Gets or sets the ISite of the Component. (Inherited from Component.)
Public property State Gets the ConnectionState property of the underlying provider if the EntityConnection is open. Otherwise, returns Closed. (Overrides DbConnection.State.)
Public property StoreConnection Provides access to the underlying data source connection that is used by the EntityConnection object.
Top
  Name Description
Protected method BeginDbTransaction Starts a database transaction. (Inherited from DbConnection.)
Public method BeginTransaction() Begins a transaction by using the underlying provider.
Public method BeginTransaction(IsolationLevel) Begins a transaction with the specified isolation level by using the underlying provider.
Public method ChangeDatabase Not supported. (Overrides DbConnection.ChangeDatabase(String).)
Public method Close Closes the connection to the database. (Overrides DbConnection.Close().)
Public method CreateCommand Creates a new instance of an EntityCommand, with the Connection set to this EntityConnection.
Protected method CreateDbCommand Creates and returns a DbCommand object associated with the current connection. (Inherited from DbConnection.)
Public method CreateObjRef Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public method Dispose() Releases all resources used by the Component. (Inherited from Component.)
Protected method Dispose(Boolean) Releases the unmanaged resources used by the Component and optionally releases the managed resources. (Inherited from Component.)
Public method EnlistTransaction Enlists this EntityConnection in the specified transaction. (Overrides DbConnection.EnlistTransaction(Transaction).)
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Releases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection. (Inherited from Component.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetLifetimeService Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Public method GetMetadataWorkspace Returns the MetadataWorkspace associated with this EntityConnection.
Public method GetSchema() Returns schema information for the data source of this DbConnection. (Inherited from DbConnection.)
Public method GetSchema(String) Returns schema information for the data source of this DbConnection using the specified string for the schema name. (Inherited from DbConnection.)
Public method GetSchema(String, String[]) Returns schema information for the data source of this DbConnection using the specified string for the schema name and the specified string array for the restriction values. (Inherited from DbConnection.)
Protected method GetService Returns an object that represents a service provided by the Component or by its Container. (Inherited from Component.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method InitializeLifetimeService Obtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected method MemberwiseClone() Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method MemberwiseClone(Boolean) Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Protected method OnStateChange Raises the StateChange event. (Inherited from DbConnection.)
Public method Open Establishes a connection to the data source by calling the underlying data provider's Open method. (Overrides DbConnection.Open().)
Public method ToString Returns a String containing the name of the Component, if any. This method should not be overridden. (Inherited from Component.)
Top
  Name Description
Public event Disposed Occurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)
Public event StateChange Occurs when the state of the event changes. (Inherited from DbConnection.)
Top
  Name Description
Explicit interface implemetation Private method IDbConnection.BeginTransaction() Begins a database transaction. (Inherited from DbConnection.)
Explicit interface implemetation Private method IDbConnection.BeginTransaction(IsolationLevel) Begins a database transaction with the specified IsolationLevel value. (Inherited from DbConnection.)
Explicit interface implemetation Private method IDbConnection.CreateCommand Creates and returns a DbCommand object that is associated with the current connection. (Inherited from DbConnection.)
Top

To avoid inadvertently putting objects such as System.Data.Common.CommandTrees and ObjectContext out of sync with their metadata, EntityConnection must lock its metadata. No changes to the connection string are allowed after the metadata is locked. The following are two scenarios in which metadata is locked:

  • The EntityConnection instance is constructed through the default constructor, or through the EntityConnection(String) constructor, which accepts a connection string. In either case the connection string might be changed multiple times before the connection is opened. Calling [M:System.Data.EntityClient.EntityConnection.Open()] or [M:System.Data.EntityClient.EntityConnection.GetMetadataWorkspace()] locks the metadata.

  • The EntityConnection instance is constructed through the EntityConnection(MetadataWorkspace, DbConnection) constructor, which accepts a MetadataWorkspace and a DbConnection. In this case, the metadata is locked at construction time. No changes to the connection string are ever allowed.

When metadata is loaded, the EntityConnection verifies that the conceptual model, the storage model, and the mapping file are all present.

For code examples, see Working with EntityClient.

.NET Framework

Supported in: 4, 3.5 SP1

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Simple Example.
I'm going to have to agree to the simple example proposal.
Poor Help
One simple example at the bottom of this article would be worth infinitely more than all the words on this page.