This question is locked. New answers and comments are not allowed.
Does Data Access have context awareness? The below code from Telerik's documentation suggests not. It seems inefficient, from both a coding and performance perspective, to write code like the demo. Is there a better way than to do this: Retrieve record to update from database, create object, let user update object, re-retrieve record from database, map user's changes to the newly created object and save changes in newly created object? With Entity Framework, I can simply re-attach the object that was updated by the user and save -- cutting out of the steps re-retrieving the data and mapping changes.
[HttpPost]
public ActionResult Edit( int id, Car carToUpdate )
{
try
{
Car originalCar = dbContext.Cars.FirstOrDefault( c => c.CarID == id );
originalCar.ABS = carToUpdate.ABS;
originalCar.AirConditioner = carToUpdate.AirConditioner;
originalCar.ASR = carToUpdate.ASR;
originalCar.Available = carToUpdate.Available;
... removed for brevity ...
dbContext.SaveChanges();
return RedirectToAction( "Index" );
}
catch
{
return View();
}
}
MVC Documentation
[HttpPost]
public ActionResult Edit( int id, Car carToUpdate )
{
try
{
Car originalCar = dbContext.Cars.FirstOrDefault( c => c.CarID == id );
originalCar.ABS = carToUpdate.ABS;
originalCar.AirConditioner = carToUpdate.AirConditioner;
originalCar.ASR = carToUpdate.ASR;
originalCar.Available = carToUpdate.Available;
... removed for brevity ...
dbContext.SaveChanges();
return RedirectToAction( "Index" );
}
catch
{
return View();
}
}
MVC Documentation