Export (0) Print
Expand All
3 out of 6 rated this helpful - Rate this topic

ObjectContext.Connection Property

Gets the connection used by the object context.

Namespace:  System.Data.Objects
Assembly:  System.Data.Entity (in System.Data.Entity.dll)
'Declaration
Public ReadOnly Property Connection As DbConnection

Property Value

Type: System.Data.Common.DbConnection
A DbConnection object that is the connection.
ExceptionCondition
ObjectDisposedException

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.  
Dim orderId As Integer = 43680

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

' Create a long-running context with the connection.  
Dim context As New AdventureWorksEntities(conn)

Try 
    ' Explicitly open the connection.  
    If conn.State <> ConnectionState.Open Then
        conn.Open()
    End If 

    ' Execute a query to return an order.  
    Dim order As SalesOrderHeader = context.SalesOrderHeaders.Where("it.SalesOrderID = @orderId", _
                                        New ObjectParameter("orderId", orderId)).Execute(MergeOption.AppendOnly).First()

    ' 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. 
    context.DeleteObject(order.SalesOrderDetails.First())

    ' Save changes.  
    If 0 < context.SaveChanges() Then
        Console.WriteLine("Changes saved.")
    End If 

    ' 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));  
    Dim detail = New SalesOrderDetail With
    {
        .SalesOrderID = 0,
        .SalesOrderDetailID = 0,
        .OrderQty = 2,
        .ProductID = 750,
        .SpecialOfferID = 1,
        .UnitPrice = CDec(2171.2942),
        .UnitPriceDiscount = 0,
        .LineTotal = 0,
        .rowguid = Guid.NewGuid(),
        .ModifiedDate = DateTime.Now
    }

    order.SalesOrderDetails.Add(detail)

    ' Save changes again.  
    If 0 < context.SaveChanges() Then
        Console.WriteLine("Changes saved.")
    End If 
Catch ex As InvalidOperationException
    Console.WriteLine(ex.ToString())
Finally 
    ' Explicitly dispose of the context and the connection. 
    context.Dispose()
    conn.Dispose()

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5 SP1

.NET Framework Client Profile

Supported in: 4

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.