public ResponseModel Guardar()
        {
            var rm = new ResponseModel();

            try
            {
                using (var ctx = new proyectoContext())
                {
                    if (this.id > 0)
                    {
                        ctx.Entry(this).State = EntityState.Modified;
                    }
                    else
                    {
                        ctx.Entry(this).State = EntityState.Added;
                    }

                    ctx.SaveChanges();
                    rm.SetResponse(true);
                }
            }
            catch (Exception)
            {
                throw;
            }

            return(rm);
        }
Exemple #2
0
        public ResponseModel Guardar(HttpPostedFileBase Foto)
        {
            var rm = new ResponseModel();

            try
            {
                using (var ctx = new proyectoContext())
                {
                    ctx.Configuration.ValidateOnSaveEnabled = false;//Indicarle al contexto que las validaciones cuando se guarde, esten desabilitadas, debido a que queremos que ignore la propiedad Password

                    var eUsuario = ctx.Entry(this);
                    eUsuario.State = EntityState.Modified;

                    if (this.Password == null)
                    {
                        eUsuario.Property(x => x.Password).IsModified = false;//Le indicamos que queremos que ignore la propiedad Password, y que no la valide
                    }
                    if (Foto != null)
                    {
                        //System.IO para usar Path
                        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;
                    }

                    ctx.SaveChanges();
                    rm.SetResponse(true);
                }
            }

            //Referencia using System.Data.Entity.Validation;
            //Podemos ver más a detalle la excepcion del EF
            //catch (DbEntityValidationException e)
            //{
            //    throw;
            //}

            catch (Exception)
            {
                throw;
            }

            return(rm);
        }
        public ResponseModel Eliminar(int id)
        {
            var rm = new ResponseModel();

            try
            {
                using (var ctx = new proyectoContext())
                {
                    this.id = id;
                    ctx.Entry(this).State = EntityState.Deleted;
                    ctx.SaveChanges();
                    rm.SetResponse(true);
                }
            }
            catch (Exception)
            {
                throw;
            }
            return(rm);
        }