public void InsertOrUpdate(Photo photo) { using (AzurePhotoManagerContext context = new AzurePhotoManagerContext()) { Photo current = null; if (photo.Id == default(int)) { // New entity current = context.Photos.FirstOrDefault<Photo>(p => p.Name == photo.Name); if (current == null) { context.Photos.Add(photo); } else { photo.Id = current.Id; context.Entry(photo).State = EntityState.Modified; } } else { // Existing entity context.Entry(photo).State = EntityState.Modified; } context.SaveChanges(); } }
public ActionResult Create(Photo photo) { if (ModelState.IsValid) { photoRepository.InsertOrUpdate(photo); photoRepository.Save(); return RedirectToAction("Index"); } else { return View(); } }