Share via


DataServiceContext.UpdateObject(Object) 메서드

정의

DataServiceContext에서 지정한 개체의 상태를 Modified로 변경합니다.

public:
 void UpdateObject(System::Object ^ entity);
public void UpdateObject (object entity);
member this.UpdateObject : obj -> unit
Public Sub UpdateObject (entity As Object)

매개 변수

entity
Object

Modified 상태에 할당할 추적되는 엔터티입니다.

예외

entitynull일 경우

entityDetached 상태에 있습니다.

예제

다음 예에서는 기존 개체를 검색 및 수정한 다음 의 DataServiceContext 메서드를 호출 UpdateObject 하여 컨텍스트의 항목을 업데이트된 것으로 표시합니다. 가 호출되면 HTTP MERGE 메시지가 데이터 서비스 SaveChanges 로 전송됩니다. 이 예제에서는 WCF Data Services 완료할 때 만들어지는 Northwind 데이터 서비스를 기반으로 서비스 참조 추가 도구에서 생성된 를 사용합니다DataServiceContext.

string customerId = "ALFKI";

// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);

// Get a customer to modify using the supplied ID.
var customerToChange = (from customer in context.Customers
                        where customer.CustomerID == customerId
                        select customer).Single();

// Change some property values.
customerToChange.CompanyName = "Alfreds Futterkiste";
customerToChange.ContactName = "Maria Anders";
customerToChange.ContactTitle = "Sales Representative";

try
{
    // Mark the customer as updated.
    context.UpdateObject(customerToChange);

    // Send the update to the data service.
    context.SaveChanges();
}
catch (DataServiceRequestException  ex)
{
    throw new ApplicationException(
        "An error occurred when saving changes.", ex);
}
Dim customerId = "ALFKI"

' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)

' Get a customer to modify using the supplied ID.
Dim customerToChange = (From customer In context.Customers _
                        Where customer.CustomerID = customerId _
                        Select customer).Single()

' Change some property values.
customerToChange.CompanyName = "Alfreds Futterkiste"
customerToChange.ContactName = "Maria Anders"
customerToChange.ContactTitle = "Sales Representative"

Try
    ' Mark the customer as updated.
    context.UpdateObject(customerToChange)

    ' Send the update to the data service.
    context.SaveChanges()
Catch ex As DataServiceRequestException
    Throw New ApplicationException( _
            "An error occurred when saving changes.", ex)
End Try

적용 대상