Partager via


DataServiceContext.AddObject Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Adds the specified object to the set of objects that the DataServiceContext is tracking.

Namespace:  System.Data.Services.Client
Assembly:  System.Data.Services.Client (in System.Data.Services.Client.dll)

Syntax

'Declaration
Public Sub AddObject ( _
    entitySetName As String, _
    entity As Object _
)
public void AddObject(
    string entitySetName,
    Object entity
)

Parameters

  • entitySetName
    Type: System.String
    The name of the entity set to which the resource will be added.

Exceptions

Exception Condition
ArgumentNullException

When entitySetName or entity is nulla null reference (Nothing in Visual Basic).

ArgumentException

When entitySetName is an empty string.

-or-

When entity does not have a key property.

InvalidOperationException

When entity is already being tracked by the context.

Remarks

The object is put into the tracking set of the DataServiceContext in the added state. The DataServiceContext will try to insert the object by HTTP POST on the next call to BeginSaveChanges. This method does not add objects related to the specified entity to the DataServiceContext. Each object must be added by making a separate call to AddObject.

The method does not validate that the entity set specified exists in the data service associated with the DataServiceContext or that the added object has the required properties needed to be added to the specified entity set.

Examples

For an example that uses the AddObject method, see Creating New Entities with Relationships (WCF Data Services/Silverlight).

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.