ObjectContext.Connection Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets the connection used by the object context.

Namespace:   System.Data.Objects
Assembly:  System.Data.Entity (in System.Data.Entity.dll)

public DbConnection Connection { get; }

Property Value

Type: System.Data.Common.DbConnection

A DbConnection object that is the connection.

Exception Condition

When the ObjectContext instance has been disposed.

This property returns the connection object that was passed into the constructor or created during construction. The connection’s ownership is determined by the constructors used.

This example creates an EntityConnection object that is passed into the constructor of a long-running ObjectContext. The connection is opened manually. Both the EntityConnection object and the ObjectContext object are disposed of manually.

// Define the order ID for the order we want.
int orderId = 43680;

// Create an EntityConnection.
EntityConnection conn =
    new EntityConnection("name=AdventureWorksEntities");

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

    // Explicitly open the connection.
    if (conn.State != ConnectionState.Open)

    // Execute a query to return an order.
    SalesOrderHeader order =
        "it.SalesOrderID = @orderId", new ObjectParameter("orderId", orderId))

    // Change the status of the order.
    order.Status = 1;

    // You do not have to call the Load method to load the details for the order,
    // because  lazy loading is set to true 
    // by the constructor of the AdventureWorksEntities object. 
    // With  lazy loading set to true the related objects are loaded when
    // you access the navigation property. In this case SalesOrderDetails.

    // Delete the first item in the order.

    // Save changes.
    if (0 < context.SaveChanges())
        Console.WriteLine("Changes saved.");

    // Create a new SalesOrderDetail object.
    // You can use the static CreateObjectName method (the Entity Framework
    // adds this method to the generated entity types) instead of the new operator:
    // SalesOrderDetail.CreateSalesOrderDetail(1, 0, 2, 750, 1, (decimal)2171.2942, 0, 0,
    //                                         Guid.NewGuid(), DateTime.Today));
    SalesOrderDetail detail = new SalesOrderDetail
        SalesOrderID = 1,
        SalesOrderDetailID = 0,
        OrderQty = 2,
        ProductID = 750,
        SpecialOfferID = 1,
        UnitPrice = (decimal)2171.2942,
        UnitPriceDiscount = 0,
        LineTotal = 0,
        rowguid = Guid.NewGuid(),
        ModifiedDate = DateTime.Now


    // Save changes again.
    if (0 < context.SaveChanges())
        Console.WriteLine("Changes saved.");
catch (InvalidOperationException ex)
    // Explicitly dispose of the context and the connection. 

.NET Framework
Available since 3.5
Return to top