EntityCollection<TEntity>::Add Method (TEntity)

 

Adds an object to the collection.

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

public:
virtual void Add(
	TEntity entity
) sealed

Parameters

entity
Type: TEntity

An object to add to the collection. entity must implement IEntityWithRelationships.

Exception Condition
ArgumentNullException

entity is null.

The Add method adds an object to an EntityCollection<TEntity> and creates a relationship between the two objects. When the source object is attached to an ObjectContext instance, the Add method also adds the object to the ObjectContext. This operation is translated into an insert operation in the data source when SaveChanges is called. For more information, see Creating, Adding, Modifying, and Deleting Objects.

The Add method can be called multiple times on the same object instance.

This example is based on the . To run the code in this example, you must have already added the AdventureWorks Sales Model to your project and configured your project to use the Entity Framework. To do this, complete the procedures in How to: Manually Configure an Entity Framework Project and How to: Manually Define the Model and Mapping Files.

This example creates two new SalesOrderHeader entities, adds them to the Contact entity, and, after removing an object, uses the Add method to add the object back to the collection.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 3.5
Return to top
Show: