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

AttachCopy to update existing entity?

2 Answers 102 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
HHalim
Top achievements
Rank 2
HHalim asked on 16 Apr 2013, 03:57 PM
How do I update an existing entity using AttachCopy method? Is it possible to do it that way?
I know I can use the regular update feature, but I'm trying to safe time and effort because some of my entity objects have over 100 fields with data coming in from 3rd party API.

I've successfully inserted new record using the AttachCopy, but not update an existing record.

Here is in code of what I'm hoping to do, this doesn't work in updating the existing object:
public ActionResult Update(UserMeta userMeta)
  {
     var entity = (from u in dbContext.UserMetas
                     where u.UserID == userMeta.userID
                     select u).FirstOrDefault();
      if (entity != null)
      {
           var entity = dbContext.AttachCopy(userMeta);
               entity.Modified = DateTime.UtcNow;
           dbContext.SaveChanges();
      }
       return View();
}


The parameter userMeta object is populated from a form view and has the updated field values. The userMeta class was generated through openaccess database first mapping, so it's the same class definition as entity object.

Thanks!

2 Answers, 1 is accepted

Sort by
0
Accepted
Yordan
Telerik team
answered on 19 Apr 2013, 11:28 AM
Hi Hartono,

As we see from the provided code the way you use AttachCopy is perfectly fine. However there is a possibility that the entity returned from the query 
var entity =
(from u in dbContext.UserMetas
where u.UserID == userMeta.userID
select u).FirstOrDefault();
is null and the rest of the code is not executed. Could you please debug the method and verify that the returned entity is not null. Additionally you could use this version of the Update method:
public ActionResult Update(UserMeta userMeta)
{
 if (userMeta != null)
 {
  var attachedUserMeta = dbContext.AttachCopy(userMeta);
  attachedUserMeta.Modified = DateTime.UtcNow;
  dbContext.SaveChanges();
 }
 return View();
}
This method will work unless the provided userMeta is null.
If you encounter any issues using this version of the Update method please do not hesitate to contact us again.
 
Regards,
Yordan
the Telerik team
Using Encrypted Connection Strings with Telerik OpenAccess ORM. Read our latest blog article >>
0
HHalim
Top achievements
Rank 2
answered on 20 Apr 2013, 02:30 AM

From your reply I figured out what the problem was. It's that I don't actually need to manually retrieve the record entity first and then do the AttachCopy. Changing my code and going straight to AttachCopy for insert and update works as intended.

Thank you!
Tags
Development (API, general questions)
Asked by
HHalim
Top achievements
Rank 2
Answers by
Yordan
Telerik team
HHalim
Top achievements
Rank 2
Share this question
or