This is a migrated thread and some comments may be shown as answers.

Context Awareness in Data Access and MVC?

1 Answer 46 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Dan
Top achievements
Rank 1
Dan asked on 14 Mar 2014, 05:29 PM
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

1 Answer, 1 is accepted

Sort by
0
Boyan
Telerik team
answered on 19 Mar 2014, 02:53 PM
Hello Dan,

Similar behavior could be achieved by our attach/detach api. After the persistent type is received as a parameter in your controller's action (or is reconstructed from a dto) it could be attached to a context. After doing so, Telerik Data Access will identify the attached object by its primary key and will start tracking it. This means that any changes made to the attached object will be persistent in the database allowing you to just call .SaveChanges() to update the entity. If a such record is not available in the database new record will be inserted.
To achieve that your controller's action could be modified in the following way:

[HttpPost]
public ActionResult Edit( int id, Car carToUpdate )
{
   try
   {
        carToUpdate.CarId = id;
        dbContext.AttachCopy(carToUpdate);
        dbContext.SaveChanges();
       
        return RedirectToAction( "Index" );
   }
   catch
   {
        return View();
   }
}

Please note that the above approach will actually execute database query to retrieve the attached object as well. This is necessary because otherwise concurrency control issue could occur. Additionally, Telerik Data Access would only update the required columns in the database. Please refer to this documentation article for more detailed information on this feature. 

Please let us know if you have any further questions.

Regards,
Boyan
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
Tags
General Discussions
Asked by
Dan
Top achievements
Rank 1
Answers by
Boyan
Telerik team
Share this question
or