Condividi tramite


Attaching Objects (Entity Framework)

When a query is executed inside an object context in the Entity Framework, the returned objects are automatically attached to the object context. You can also attach objects to an object context that are obtained from a source other than a query. You might attach objects that have been previously detached, objects that have been returned by a NoTracking query, or objects that have been obtained outside the object context. You can also attach objects that have been stored in the view state of an ASP.NET application or have been returned from a remote method call or Web service.

Use one of the following methods to attach the object to an object context:

  • Call AddObject on 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.

  • Call Attach on ObjectContext to attach the object to the object context. Do this when the object already exists in the data source but is currently not attached to the context. For more information, see How to: Attach Related Objects (Entity Framework).

  • Call AttachTo on ObjectContext to attach the object to a specific entity set in the object context. Also do this if the object has a null (Nothing in Visual Basic) EntityKey value.

  • Call ApplyPropertyChanges on ObjectContext. Do this when the object already exists in the data source and the detached object has property updates that you want to persist. If you merely attach the object, property changes are lost. For more information, see How to: Apply Changes Made to a Detached Object (Entity Framework).

The following considerations apply when attaching objects to the object context:

  • If the object being attached has related objects, those objects are also attached to the object context.

  • To use Attach to attach an object, the object must implement IEntityWithKey and have a valid key.

  • Objects are attached to the object context in an Unchanged state.

  • If the attached object does not exist in the data source, it is not added during SaveChanges. In this case, when changes are made to properties, an exception occurs at the server during SaveChanges. To add an object, use AddObject instead of Attach.

  • If the object being attached is related to other objects, you must explicitly define the relationships in one of the following ways:

    • Attach both objects to the object context, and then call Attach on EntityCollection or EntityReference to define the relationship.

    • If neither object is attached, call Add on EntityCollection and specify the related object or set the Value property of EntityReference to the related object. Next, attach the root of the object graph to the object context. You can use this method to construct an object graph from detached objects and then attach the graph to the object context.

    These methods are used when attaching related objects that were serialized by using XML serialization. For more information, see How to: Attach Related Objects (Entity Framework).

  • If the object being attached has updated property values, use ApplyPropertyChanges to apply the updates to the existing object. For more information, see How to: Apply Changes Made to a Detached Object (Entity Framework).

  • The object that is passed to the Attach method must have a valid EntityKey value. If the object does not have a valid EntityKey value, use the AttachTo method to specify the name of the entity set.

  • An InvalidOperationException occurs when an object being attached has the same EntityKey as a different object already present in the object context. This error does not occur when the same object instance is already in the object context; Attach can be called multiple times on the same object instance as long as the object is in the Unchanged state.

  • Use the AttachTo method to attach objects to a specific entity set. For more information, see Adding, Modifying, and Deleting Objects (Entity Framework).

  • When the object being attached by using the AttachTo method already has its entity key defined, an InvalidOperationException occurs if the value of the entitySetName parameter does not match the entity set name in the existing key.

See Also

Concepts

Detaching Objects (Entity Framework)
Web Services and the Entity Data Model (Application Scenarios)

Other Resources

Managing the Object Context (Entity Framework)