Which code segment should you use?

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to develop an application.
You use the ADO.NET Entity Framework Designer to model entities. You need to associate a
previously deserialized entity named personl to an object context named model and persist changes
to the database. Which code segment should you use?

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to develop an application.
You use the ADO.NET Entity Framework Designer to model entities. You need to associate a
previously deserialized entity named personl to an object context named model and persist changes
to the database. Which code segment should you use?

A.
personl.AcceptChanges();
mode1.SaveChanges();

B.
model.People.ApplyChanges(personl);
mode1.SaveChanges();

C.
model.AttachTo(“People”, personl);
model.SaveChanges();

D.
model.People.Attach(personl);
model.SaveChanges();

Explanation:
Cosiderations from Attaching and Detaching objects
(http://msdn.microsoft.com/en-us/library/bb896271.
aspx):
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.
Attach Use the Attach method of ObjectContext where the method accepts a single typed entity
parameter.
AttachTo The AttachTo method of ObjectContext accepts two parameters. The first parameter is a
string containing the name of the entity set.
The second parameter’ type is object and references the entity you want to add.
Attach The Attach method of ObjectSet, which is the entity set’ type, accepts a single typed
parameter containing the entity to be added to the ObjectSet.
CHAPTER 6 ADO.NET Entity Framework
Lesson 2: Querying and Updating with the Entity Framework
Attaching Entities to an ObjectContext(page 437)
Attaching and Detaching objects
(http://msdn.microsoft.com/en-us/library/bb896271.aspx)
http://msdn.microsoft.com/en-us/library/bb896248(v=vs.90).aspx
http://msdn.microsoft.com/en-us/library/bb896248.aspx



Leave a Reply 0

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