Export (0) Print
Expand All

ObjectContext::AttachTo Method

Attaches an object or object graph to the object context in a specific entity set.

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

public:
void AttachTo(
	String^ entitySetName, 
	Object^ entity
)

Parameters

entitySetName
Type: System::String
Represents the entity set name, which may optionally be qualified by the entity container name.
entity
Type: System::Object
The Object to attach.

ExceptionCondition
ArgumentNullException

The entity is nullptr.

InvalidOperationException

Invalid entity set.

-or-

The object has a temporary key.

-or-

The object has an EntityKey and the EntitySet does not match with the entity set passed in as an argument of the method.

-or-

The object does not have an EntityKey and no entity set is provided.

-or-

Any object from the object graph has a temporary EntityKey.

-or-

Any object from the object graph has an invalid EntityKey (for example, values in the key do not match values in the object).

-or-

The entity set could not be found from a given entitySetName name and entity container name.

-or-

Any object from the object graph already exists in another state manager.

Call AttachTo on the ObjectContext to attach the object to a specific entity set in the object context or if the object has a nullptr (Nothing in Visual Basic) EntityKey value. For more information, see Attaching Objects (Entity Framework).

The object being attached can be a single object or be part of the graph.

The rules for the entitySetName format are as follows:

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

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

The object being attached is not required to have an EntityKey associated with it. If the object does not have an entity key, then entitySetName cannot be an empty string.

If the object being attached 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.

.NET Framework

Supported in: 4, 3.5 SP1

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft