本主题提供有关如何提供现有 EntityConnection 以供对象上下文使用的示例。有关更多信息,请参见管理对象上下文(实体框架)。
本主题中的示例基于 AdventureWorks 销售模型 (EDM)。若要运行本示例中的代码,必须已将 AdventureWorks 销售模型添加到您的项目中,并将项目配置为使用 Entity Framework。为此,请完成如何:使用实体数据模型向导(实体框架) 中的过程。
示例
本示例创建一个 EntityConnection,它将被传递到长时间运行的 ObjectContext 的构造函数中。此连接以手动方式建立。EntityConnection 和 ObjectContext 均以手动方式释放。
' Define the order ID for the order we want.
Dim orderId As Integer = 43661
' Create an EntityConnection.
Dim conn As New EntityConnection("name=AdventureWorksEntities")
' Create a long-running context with the connection.
Dim advWorksContext As New AdventureWorksEntities(conn)
Try
    ' Explicitly open the connection.
    If Not conn.State = ConnectionState.Open Then
        conn.Open()
    End If
    ' Execute a query to return an order.
    Dim order As SalesOrderHeader = _
        advWorksContext.SalesOrderHeader.Where( _
        "it.SalesOrderID = @orderId", New ObjectParameter("orderId", orderId)) _
        .Execute(MergeOption.AppendOnly).First()
    ' Change the status of the order.
    order.Status = 1
    ' Save changes.
    If 0 < advWorksContext.SaveChanges() Then
        Console.WriteLine("Changes saved.")
    End If
    ' Load the order's items.
    order.SalesOrderDetail.Load()
    ' Delete the first item.
    advWorksContext _
        .DeleteObject(order.SalesOrderDetail.First())
    ' Save changes again.
    If 0 < advWorksContext.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. 
    advWorksContext.Dispose()
    conn.Dispose()
End Try
// Define the order ID for the order we want.
int orderId = 43661;
// Create an EntityConnection.
EntityConnection conn =
    new EntityConnection("name=AdventureWorksEntities");
// Create a long-running context with the connection.
AdventureWorksEntities advWorksContext =
    new AdventureWorksEntities(conn);
try
{
    // Explicitly open the connection.
    if (conn.State != ConnectionState.Open)
    {
        conn.Open();
    }
    // Execute a query to return an order.
    SalesOrderHeader order =
        advWorksContext.SalesOrderHeader.Where(
        "it.SalesOrderID = @orderId", new ObjectParameter("orderId", orderId))
        .Execute(MergeOption.AppendOnly).First();
    // Change the status of the order.
    order.Status = 1;
    // Save changes.
    if (0 < advWorksContext.SaveChanges())
    {
        Console.WriteLine("Changes saved.");
    }
    // Load the order's items.
    order.SalesOrderDetail.Load();
    // Delete the first item.
    advWorksContext
        .DeleteObject(order.SalesOrderDetail.First());
    // Save changes again.
    if (0 < advWorksContext.SaveChanges())
    {
        Console.WriteLine("Changes saved.");
    }
}
catch (InvalidOperationException ex)
{
    Console.WriteLine(ex.ToString());
}
finally
{
    // Explicitly dispose of the context and the connection. 
    advWorksContext.Dispose();
    conn.Dispose();
}