//metodo guardar public void Guardar()//retorna solo un objeto { try { using (var db = new Modelo_Sistemas()) { if (this.actividad_id > 0) { //si existe un valor mayor a 0 es porque existe un registro db.Entry(this).State = EntityState.Modified; } else { //si no existe registro graba(nuevo registro) db.Entry(this).State = EntityState.Added; } db.SaveChanges(); } } catch (Exception ex) { throw; } }
//Metodo Actualizar perfil Personal public ResponseModel GuardarPerfil(HttpPostedFileBase Foto) { var rm = new ResponseModel(); try { using (var db = new Modelo_Sistemas()) { db.Configuration.ValidateOnSaveEnabled = false; var Usu = db.Entry(this); Usu.State = EntityState.Modified; if (Foto != null) { string extension = Path.GetExtension(Foto.FileName).ToLower(); int size = 1024 * 1024 * 5; var filtroextension = new[] { ".jpg", ".jpeg", ".png", ".gif" }; var extensiones = Path.GetExtension(Foto.FileName); if (filtroextension.Contains(extensiones) && (Foto.ContentLength <= size)) { String archivo = Path.GetFileName(Foto.FileName); Foto.SaveAs(HttpContext.Current.Server.MapPath("~/Imagenes/" + archivo)); this.avatar = archivo; } } else { Usu.Property(x => x.avatar).IsModified = false; } if (this.usuario_id == 0) { Usu.Property(x => x.usuario_id).IsModified = false; } if (this.docente_id == 0) { Usu.Property(x => x.docente_id).IsModified = false; } if (this.nombre == null) { Usu.Property(x => x.nombre).IsModified = false; } if (this.clave == null) { Usu.Property(x => x.clave).IsModified = false; } if (this.nivel == null) { Usu.Property(x => x.nivel).IsModified = false; } if (this.estado == null) { Usu.Property(x => x.estado).IsModified = false; } db.SaveChanges(); rm.SetResponse(true); } } catch (DbEntityValidationException) { throw; } catch (Exception) { throw; } return(rm); }