//Metodo Eliminar public void Eliminar() { try { using (var db = new Modelo_Sistema()) { db.Entry(this).State = EntityState.Deleted; //elimina el registro db.SaveChanges(); } } catch (Exception ex) { throw; } }
//Metodo Guardar public void Guardar() { try { using (var db = new Modelo_Sistema()) { if (this.USUid > 0) { //si existe un valor mayor a cero, es porque existe el registro db.Entry(this).State = EntityState.Modified; } else { //si no existe el registro lo guarda(nuevo) db.Entry(this).State = EntityState.Added; } db.SaveChanges(); } } catch (Exception ex) { throw; } }
//Metodod Actualizar Perfil Personal public spmClsResponseModel GuardarPerfil(HttpPostedFileBase USUfoto) { var rm = new spmClsResponseModel(); var objUsuario = new spmUSUtUsuario(); try { using (var db = new Modelo_Sistema()) { db.Configuration.ValidateOnSaveEnabled = false; var Usu = db.Entry(this); Usu.State = EntityState.Modified; if (USUfoto != null) { string extension = Path.GetExtension(USUfoto.FileName).ToLower(); int size = 1024 * 1024 * 5; var filtroextension = new[] { ".jpg", ".jpeg", ".png", ".gif", ".PNG", ".JPEG" }; var extensiones = Path.GetExtension(USUfoto.FileName); if (filtroextension.Contains(extensiones) && (USUfoto.ContentLength <= size)) { string archivo = Path.GetFileName(USUfoto.FileName); USUfoto.SaveAs(HttpContext.Current.Server.MapPath("~/Archivos/" + archivo)); this.USUfoto = archivo; } } else { Usu.Property(x => x.USUfoto).IsModified = false; } if (this.USUid == 0) { Usu.Property(x => x.USUid).IsModified = false; } if (this.USUnombre == null) { Usu.Property(x => x.USUnombre).IsModified = false; } if (this.USUapellido == null) { Usu.Property(x => x.USUapellido).IsModified = false; //true para permitir cambiar } if (this.USUnombre_usuario == null) { Usu.Property(x => x.USUnombre_usuario).IsModified = false; } if (this.USUcontraseña == null) { Usu.Property(x => x.USUcontraseña).IsModified = false; } if (this.USUcorreo_electronico == null) { Usu.Property(x => x.USUcorreo_electronico).IsModified = false; } db.SaveChanges(); rm.SetResponse(true); } } catch (DbEntityValidationException e) { throw; } catch (Exception) { throw; } return(rm); }