public int UploadImageInDataBase(HttpPostedFileBase file, Emp _Emp) { using (var context = new TAS2013Entities()) { List<EmpPhoto> _empPhotoList = new List<EmpPhoto>(); EmpPhoto _EmpPhoto = new EmpPhoto(); int empPhotoID = 0; _empPhotoList = context.EmpPhotoes.Where(aa => aa.EmpID == _Emp.EmpID).ToList(); _EmpPhoto.EmpPic = ConvertToBytes(file); if (_empPhotoList.Count > 0) { _EmpPhoto.PhotoID = _empPhotoList.FirstOrDefault().PhotoID; _empPhotoList.FirstOrDefault().EmpPic = _EmpPhoto.EmpPic; context.EmpPhotoes.Attach(_empPhotoList.FirstOrDefault()); context.Entry(_empPhotoList.FirstOrDefault()).State = EntityState.Modified; context.SaveChanges(); } else { //Add New Image _EmpPhoto.EmpID = _Emp.EmpID; context.EmpPhotoes.Add(_EmpPhoto); } try { empPhotoID = _EmpPhoto.PhotoID; context.SaveChanges(); return empPhotoID; } catch (Exception ex) { return empPhotoID; } } }