Which method should you call on the ObjectContext?

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to create an application. The
application connects to a Microsoft SQL Server database. The application uses the ADO.NET Entity
Framework to manage order data. The application makes a Web service call to obtain orders from an
order-tracking system. You need to ensure that the orders are added to the local data store. Which
method should you call on the ObjectContext?

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to create an application. The
application connects to a Microsoft SQL Server database. The application uses the ADO.NET Entity
Framework to manage order data. The application makes a Web service call to obtain orders from an
order-tracking system. You need to ensure that the orders are added to the local data store. Which
method should you call on the ObjectContext?

A.
Attach

B.
AttachTo

C.
AddObject

D.
ApplyCurrentValues

Explanation:
ObjectContext.AddObject() 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.
ObjectContext.Attach() Call Attach on the 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.
If the object being attached has related objects, those objects will also be attached to the object
context.
Objects are added to the object context in an unchanged state.
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.
ObjectContext.AttachTo() Call AttachTo on the ObjectContext to attach the object to a specific entity
set in the object context or if the object has a null (Nothing in Visual Basic) EntityKey value.
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.
ApplyCurrentValues<TEntity>() method is used to apply changes that were made to objects outside
the ObjectContext, such as detached objects that are received by a Web service.

The method copies the scalar values from the supplied object into the object in the ObjectContext
that has the same key.
You can use the EntityKey of the detached object to retrieve an instance of this object from the data
source.



Leave a Reply 0

Your email address will not be published. Required fields are marked *