ObjectContext.AddObject Method (String, Object)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Adds an object to the object context.

Namespace:   System.Data.Objects
Assembly:  System.Data.Entity (in System.Data.Entity.dll)

Public Sub AddObject (
	entitySetName As String,
	entity As Object


Type: System.String

Represents the entity set name, which may optionally be qualified by the entity container name.

Type: System.Object

The Object to add.

Exception Condition

The entity parameter is null.


The entitySetName does not qualify.

Call AddObject on the ObjectContext to add the object to the object context. Do this when the object is a new object that does not yet exist in the data source. For more information, see Attaching and Detaching Objects.

Objects are added to the ObjectStateManager in the Detached, Deleted or Added state.

When you create a new object that is related to another object in the object context, add the object by using one of the following methods:

For more information, see Creating, Adding, Modifying, and Deleting Objects.

If the object is in a detached state it must not have an EntityKey.

The rules for the entitySetName format are as follows:

  • If the DefaultContainerName property is null, then the entitySetName has to be fully qualified as in <Entity Container Name>.<Entity Set Name>.

  • If DefaultContainerName is not null, then the entitySetName can be either <Entity Container Name>.<Entity Set Name> or <Entity Set Name>.

If the object has an EntityKey and entitySetName has a value, then the EntitySet of the entity key must match the EntitySet that was found based on the entitySetName and entity container name.

This example is based on the AdventureWorks Sales Model. The example adds a new product and saves the changes to the database.

Dim newProduct As Product

' Define values for the new product. 
Dim dateTimeString As String = "1998-06-01 00:00:00.000"
Dim productName As String = "Flat Washer 10"
Dim productNumber As String = "FW-5600"
Dim safetyStockLevel As Int16 = 1000
Dim reorderPoint As Int16 = 750

' Convert the date time string into a DateTime instance. 
Dim sellStartDate As DateTime
If Not DateTime.TryParse(dateTimeString, sellStartDate) Then
    Throw New ArgumentException(String.Format("The string '{0}'cannot be converted to DateTime.", dateTimeString))
End If

' Create a new Product. 
newProduct = Product.CreateProduct(0, productName, productNumber, False, False, safetyStockLevel, _
             reorderPoint, 0, 0, 0, DateTime.Today, Guid.NewGuid(), DateTime.Today)

Using context As New AdventureWorksEntities()
        ' Add the new object to the context. 

        ' Persist the new produc to the data source. 

        ' Return the identity of the new product. 
        Return newProduct.ProductID
    Catch ex As UpdateException
        Throw New InvalidOperationException(String.Format("The object could not be added. Make sure that a " & _
                                                          "product with a product number '{0}' does not aleady exist.", _
                                                          newProduct.ProductNumber), ex)
    End Try
End Using

.NET Framework
Available since 3.5
Return to top