共用方式為


HOW TO:在長時間執行的物件內容中管理連接 (Entity Framework)

本主題提供的範例將示範如何針對長時間執行的物件內容手動開啟連接。 此範例也會示範如何在此內容上呼叫 Dispose,以確保連接已釋放。 如需詳細資訊,請參閱管理物件內容 (Entity Framework)

本主題的範例是根據 AdventureWorks Sales Model (EDM)。 若要執行此範例中的程式碼,您必須已經將 AdventureWorks Sales Model 加入到專案中,並設定您的專案使用 Entity Framework。 若要這樣做,請完成 HOW TO:使用 Entity Data Model 精靈 (Entity Framework) 中的程序。

範例

此範例會手動開啟長時間執行之 ObjectContext 所使用的連接,然後執行查詢及儲存變更。 當處置 ObjectContext 時,就會關閉連接。

' Define the order ID for the order we want.
Dim orderId As Integer = 43661

' Create a long-running context.
Dim advWorksContext As New AdventureWorksEntities()
Try
    ' Explicitly open the connection.
    If Not advWorksContext.Connection.State = ConnectionState.Open Then
        advWorksContext.Connection.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, 
    ' which closes the connection. 
    advWorksContext.Dispose()
End Try
// Define the order ID for the order we want.
int orderId = 43661;

// Create a long-running context.
AdventureWorksEntities advWorksContext =
    new AdventureWorksEntities();
try
{
    if (advWorksContext.Connection.State != ConnectionState.Open)
    {
        // Explicitly open the connection.
        advWorksContext.Connection.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, 
    // which closes the connection. 
    advWorksContext.Dispose();
}

另請參閱

工作

HOW TO:在長時間執行的物件內容中管理連接 (Entity Framework)
HOW TO:從物件內容手動開啟連接 (Entity Framework)