방법: 개체 컨텍스트에서 EntityConnection 사용(Entity Framework)

이 항목에서는 개체 컨텍스트에서 사용하도록 기존 EntityConnection을 제공하는 방법에 대한 예제를 제공합니다. 자세한 내용은 개체 컨텍스트 관리(Entity Framework)를 참조하십시오.

이 항목의 예제는 AdventureWorks Sales 모델(EDM)을 기반으로 합니다. 이 예제의 코드를 실행하려면 프로젝트에 AdventureWorks Sales 모델을 추가하고 Entity Framework를 사용하도록 프로젝트를 구성해야 합니다. 이렇게 하려면 방법: 엔터티 데이터 모델 마법사 사용(Entity Framework)의 절차를 수행합니다.

예제

이 예제에서는 장기 실행 ObjectContext의 생성자에 전달되는 EntityConnection을 만듭니다. 이 연결은 수동으로 열립니다. EntityConnectionObjectContext는 모두 수동으로 삭제됩니다.

' 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();
}

참고 항목

작업

방법: 장기 실행 개체 컨텍스트의 연결 관리(Entity Framework)
방법: 수동으로 개체 컨텍스트의 연결 열기(Entity Framework)