예제 #1
0
        public override bool Modificar(Listas lista)
        {
            RepositorioLista repositorioVentas = new RepositorioLista(new Contexto());
            Contexto         contextoLista     = new Contexto();
            bool             pasoLista         = false;

            try
            {
                Contexto contexto = new Contexto();
                bool     paso     = false;
                var      anterior = repositorioVentas.Buscar(lista.ListaId);
                foreach (var item in anterior.DetalleListas.ToList())
                {
                    if (!lista.DetalleListas.Exists(e => e.DetalleListaId == item.DetalleListaId))
                    {
                        contexto.Clientes.Find().CantidaArticulo += item.Cantidad;
                        contexto.Entry(item).State = System.Data.Entity.EntityState.Deleted;
                        paso = true;
                    }
                }
                if (paso)
                {
                    contexto.SaveChanges();
                    contexto.Dispose();
                }

                foreach (var item in lista.DetalleListas)
                {
                    var estado = EntityState.Unchanged;
                    if (item.DetalleListaId == 0)
                    {
                        estado = EntityState.Added;
                    }
                    contextoLista.Entry(item).State = estado;
                    contextoLista.Clientes.Find(item.ClienteId).CantidaArticulo -= item.Cantidad;
                }
                contextoLista.Entry(lista).State = EntityState.Modified;
                if (contextoLista.SaveChanges() > 0)
                {
                    pasoLista = true;
                }
            }
            catch (Exception)
            {
                throw;
            }
            return(pasoLista);
        }