Context Awareness in Data Access and MVC?

2 posts, 0 answers
  1. Dan
    Dan avatar
    12 posts
    Member since:
    Jan 2014

    Posted 14 Mar 2014 Link to this post

    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
  2. Boyan
    Admin
    Boyan avatar
    100 posts

    Posted 19 Mar 2014 Link to this post

    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.
     
  3. DevCraft banner
Back to Top