1

I am developing an application in MVC for Warehouse management, I have to update my inventory table, specifically the Stock field, when I receive the products I need to increase the Stock field and when I send products I need to discount the Stock field, I am only able to add a new row) and what I want is to update the row (Field Stock only) attached photo Table Inventario

My Controller [Post]:

    //CREAMOS/DECLARAMOS LA TRANSACCION
    using (var transaccion = db.Database.BeginTransaction())
    {
        try
        {
            //CARGO LOS DATOS A RECEPCION
            Recepcion recepcion = new Recepcion
            {
                Kn_CodigoProveedor = proveeid,
                Kn_CodigoBodega = bodeid,
                UserId = usuaid,
                f_Ingreso = fingreso,
                f_Factura = ffactura,
                f_Guia = fguia,
                n_Guia = nguia,
                n_Factura = nfactura,
                n_OrdenCompra = nordencompra,                        
            };

            db.Recepcions.Add(recepcion);
            db.SaveChanges();

            //RECUPERO EL ULTIMO ID QUE GENERO (ULTIMA RECEPCION)
            ultimarecepcionid = db.Recepcions.ToList().Select(r => r.Kn_CodigoRecepcion).Max();

           //CICLO QUE GUARDA CADA ELEMENTO DEL DETALLE
            foreach (ProductosRecepcion item in recepcionview.ProductosList)
            { 
                var detalle = new RecepcionDetalle()
                {               
                    Kn_CodigoRecepcion = ultimarecepcionid,
                    Kn_CodigoProducto = item.Kn_CodigoProducto,
                    Foto = item.Foto,
                    d_Cantidad = item.d_Cantidad,
                    Precio_Unitario = item.Precio_Unitario,                           
                };                                               

                //CARGO EL OBJETO AL DETALLE
                db.RecepcionDetalles.Add(detalle);

                foreach (ProductosRecepcion item1 in recepcionview.ProductosList)
                {
                    //RECUPERO ULTIMO STOCK (TABLA INVENTARIO)
                    UltimoStock = db.Inventarios.Where(b => b.Kn_CodigoBodega == bodeid).Where(p => p.Kn_CodigoProducto == item.Kn_CodigoProducto).ToList().Select(p => p.Stock).Max();

                }

                //ARMO EL OBJETO PARA ACTULIZAR MI TABLA INVENTARIO
                var stock = new Inventario()
                {

                    Kn_CodigoProducto = item.Kn_CodigoProducto,
                    Kn_CodigoBodega = bodeid,
                    Stock = (detalle.d_Cantidad + UltimoStock),
                };

                //GUARDO
                db.Inventarios.Add(stock);
            }

            db.SaveChanges();

            //CONFIRMAMOS EXITO DE TRANSACCION
            transaccion.Commit();
        }

        catch (Exception ex)
        {
            //CONFIRMAMOS FRACASO DE TRANSACCION
            transaccion.Rollback();
            ViewBag.Error = "ERROR: " + ex.Message;

            return View(recepcionview);
        }
     }    

Sorry for my english, any help for me?

1 Answers1

0

According to what I understood, you have to update the stock levels by increasing or decreasing the "Stock" column.

However you are inserting new records in your code:

            var stock = new Inventario()
            {

                Kn_CodigoProducto = item.Kn_CodigoProducto,
                Kn_CodigoBodega = bodeid,
                Stock = (detalle.d_Cantidad + UltimoStock),
            };

            //GUARDO
            db.Inventarios.Add(stock);

This is what you should do:

  • Get the record that you want to update (by retrieving it)

    var stockItem = db.Inventarios(s => s.ReplaceBYYourField == replaceBYYourValue).FirstOrDefault();

  • Update the record by increasing or decreasing the stock value:

    stockItem.Stock +=1;

  • Save Changes:

    db.Entry(stockItem).State = System.Data.Entity.EntityState.Modified;
    db.SaveChanges();

The code follows EF version 4. In newer versions the syntax might be different but the same logic applies in the steps I mentioned. Let me know which EF version you are using so I can update my answer.

Hussein Salman
  • 7,806
  • 15
  • 60
  • 98
  • Then you have to use the Attach method, check these links [EF5](https://msdn.microsoft.com/en-us/library/jj592676(v=vs.113).aspx) and this [SO](https://stackoverflow.com/questions/15336248/entity-framework-5-updating-a-record) – Hussein Salman Oct 11 '17 at 14:51