Editing Records and Updating Images with Entity Framework

Editing records using EF is very simple. Visual Studio even scaffolds out all of the code for you if you are simply updating a textual record. However, if you are also trying to update an image or file things get a little tricky. Trying to determine if a new file has been upload and then either delete the old image and upload the new one or repopulate the old image name so that it is not removed when the new information is updated has proven to be quite a challenge. This is because of the way the EF tracks objects. It seems that we could simply use the Find() method to look up the old image however that causes the following error:

Attaching an entity of type ‘X’ failed because another entity of the same type already has the same primary key value. This can happen when using the ‘Attach’ method or setting the state of an entity to ‘Unchanged’ or ‘Modified’ if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the ‘Add’ method or the ‘Added’ entity state to track the graph and then set the state of non-new entities to ‘Unchanged’ or ‘Modified’ as appropriate.

To get around this problem we have to use a special AsNoTrackting() method to get the current photo or file name without creating a second entity for EF to track which is what the Find() method does. The following method is from a PetRepository class which has the DbContext (as context) passed in as a parameter. We use this context to query the database for the current photo/file name before we update the record.

public Pet UntrackedFind(int id)
{
return context.Set<Pet>().AsNoTracking().Where(p => p.PetID == id).FirstOrDefault();
}

Next we simply call this method in from the HttpPost version of the Edit ActionResult to update the record and either delete the old photo/file and add the new one or to populate the photo/file property with the exiting file name so that it is not set to an empty string when the rest of the entity is updated.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "PetID,OwnerID,Name,PetPhoto,SpecialNotes,IsActive,DateAdded")] Pet pet, HttpPostedFileBase petPhoto)
{

if (ModelState.IsValid)
{

string oldFileName = uow.PetRepository.UntrackedFind(pet.PetID).PetPhoto;

if (petPhoto != null)
{

var savePath = Server.MapPath("~/Content/Images/");
ImageUtilities.Delete(savePath, oldFileName);

var imageName = petPhoto.FileName;

// Begin code to upload file or image
// The following is a call to a custom ImageUtilities class which resizes
// the image, creates a thumbnail and uploads both to the server.
ImageUtilities.ResizeImage(savePath, imageName, Image.FromStream(petPhoto.InputStream), 500, 75);
// End code to upload file or image

pet.PetPhoto = imageName;
}
else
{
pet.PetPhoto = oldFileName;
}

uow.PetRepository.Update(pet);
uow.Save();
return RedirectToAction("Index");
}

return View(pet);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s