Which code segment should you use?

You are implementing an ASP.NET application that uses LINQ to Entities to access and update the
database. The application includes the following method to update a detached entity of type Person.
private NorthwindContext _entities;
public void UpdatePerson(Person personToEdit)
{}
You need to implement the UpdatePerson method to update the database row that corresponds to
the personToEdit object. Which code segment should you use?

You are implementing an ASP.NET application that uses LINQ to Entities to access and update the
database. The application includes the following method to update a detached entity of type Person.
private NorthwindContext _entities;
public void UpdatePerson(Person personToEdit)
{}
You need to implement the UpdatePerson method to update the database row that corresponds to
the personToEdit object. Which code segment should you use?

A.
_entities.People.Attach(personToEdit);
_entities.ObjectStateManager.ChangeObjectState(
personToEdit,
EntityState.Modified);
_entities.SaveChanges();

B.
_entities.ObjectStateManager.ChangeObjectState(
personToEdit,
EntityState.Added);
_entities.SaveChanges();

C.
_entities.People.ApplyCurrentValues(personToEdit);
_entities.SaveChanges();
_entities.People.Attach(new Person() { Id = personToEdit.Id });

D.
_entities.ObjectStateManager.ChangeObjectState(
personToEdit,
EntityState.Modified);
_entities.SaveChanges();

Explanation:
Table(Of TEntity).Attach Method (TEntity)
(http://msdn.microsoft.com/en-us/library/bb300517.aspx)



Leave a Reply 0

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