public ResponseModel Guardar(HttpPostedFileBase Foto) { var rm = new ResponseModel(); try { using (var ctx = new PortafolioContext()) { ctx.Configuration.ValidateOnSaveEnabled = false; var eUsuario = ctx.Entry(this); eUsuario.State = EntityState.Modified; // Campos que queremos ignorar if (Foto != null) { // Nombre del archivo, es decir, lo renombramos para que no se repita nunca string archivo = DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetExtension(Foto.FileName); // La ruta donde lo vamos guardar Foto.SaveAs(HttpContext.Current.Server.MapPath("~/uploads/" + archivo)); // Establecemos en nuestro modelo el nombre del archivo this.Foto = archivo; } else { eUsuario.Property(x => x.Foto).IsModified = false; } if (this.Password == null) { eUsuario.Property(x => x.Password).IsModified = false; } ctx.SaveChanges(); rm.SetResponse(true); } } catch (DbEntityValidationException e) { throw; } catch (Exception e) { throw; } return(rm); }
public ResponseModel Guardar(HttpPostedFileBase Foto) { var rm = new ResponseModel(); try { using (var ctx = new PortafolioContext()) { ctx.Configuration.ValidateOnSaveEnabled = false; var eUsuario = ctx.Entry(this); eUsuario.State = EntityState.Modified; if (Foto != null) { string archivo = DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetExtension(Foto.FileName); Foto.SaveAs(HttpContext.Current.Server.MapPath("~/uploads/" + archivo)); this.Foto = archivo; } else { eUsuario.Property(x => x.Foto).IsModified = false; } if (this.Password == null) { eUsuario.Property(x => x.Password).IsModified = false; } ctx.SaveChanges(); rm.SetResponse(true); } } catch (Exception e) { rm.SetResponse(false); throw; } return(rm); }