Procedura: connettere un'entità esistente a DataServiceContext (WCF Data Services)How to: Attach an Existing Entity to the DataServiceContext (WCF Data Services)

Quando un'entità già esistente in un servizio dati, il WCF Data ServicesWCF Data Services libreria client consente di connettere un oggetto che rappresenta l'entità direttamente il DataServiceContext senza prima eseguire una query.When an entity already exists in a data service, the WCF Data ServicesWCF Data Services client library enables you to attach an object that represents the entity directly to the DataServiceContext without first executing a query. Per ulteriori informazioni, vedere l'aggiornamento del servizio dati.For more information, see Updating the Data Service.

Nell'esempio riportato in questo argomento vengono usati il servizio dati Northwind di esempio e le classi del servizio dati client generate automaticamente.The example in this topic uses the Northwind sample data service and autogenerated client data service classes. Questo servizio e le classi di dati client vengono create quando si completa la Guida rapida di WCF Data Services.This service and the client data classes are created when you complete the WCF Data Services quickstart.

EsempioExample

Nell'esempio seguente viene illustrato come creare un oggetto Customer esistente contenente modifiche da salvare nel servizio dati.The following example shows how to create an existing Customer object that contains changes to be saved to the data service. oggetto viene collegato al contesto e viene chiamato il metodo UpdateObject per contrassegnare l'oggetto collegato come Modified prima della chiamata del metodo SaveChanges.The object is attached to the context and the UpdateObject method is called to mark the attached object as Modified before the SaveChanges method is called.

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

// Define an existing customer to attach, including the key.
Customer customer = 
    Customer.CreateCustomer("ALFKI", "Alfreds Futterkiste");

// Set current property values.
customer.Address = "Obere Str. 57";
customer.City = "Berlin";
customer.PostalCode = "12209";
customer.Country = "Germany";

// Set property values to update.
customer.ContactName = "Peter Franken";
customer.ContactTitle = "Marketing Manager";
customer.Phone = "089-0877310";
customer.Fax = "089-0877451";

try
{
    // Attach the existing customer to the context and mark it as updated.
    context.AttachTo("Customers", customer);
    context.UpdateObject(customer);

    // Send updates to the data service.
    context.SaveChanges();
}
catch (DataServiceClientException ex)
{
    throw new ApplicationException(
        "An error occurred when saving changes.", ex);
}
' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)

' Define an existing customer to attach, including the key.
Dim customer As Customer = _
    customer.CreateCustomer("ALFKI", "Alfreds Futterkiste")

' Set current property values.
customer.Address = "Obere Str. 57"
customer.City = "Berlin"
customer.PostalCode = "12209"
customer.Country = "Germany"

' Set property values to update.
customer.ContactName = "Peter Franken"
customer.ContactTitle = "Marketing Manager"
customer.Phone = "089-0877310"
customer.Fax = "089-0877451"

Try
    ' Attach the existing customer to the context and mark it as updated.
    context.AttachTo("Customers", customer)
    context.UpdateObject(customer)

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

Vedere ancheSee Also

Libreria client WCF Data ServicesWCF Data Services Client Library