0

I have a dictionary detalles var movimiento_det = new MovimientoDet(); //

   foreach (var detalle in detalles)
                            {
                                movimiento_det.MovimientoDetId =   G.Serial("movimiento_det"); // get me the id of the entity
                                movimiento_det.MovimientoDetId= i;
                                movimiento_det.ProductoId = detalle.Value.ProductoId;
                                movimiento_det.Cantidad = detalle.Value.Cantidad;
                                Vmsb.MovimientoDets.Add(movimiento_det);
                            }
                            Vmsb.SaveChanges();

I want to save all values of the dictonary but it just saving to the database the last data in the foreach how can i do to save all the data

using ef5, and vs2012

1 Answer 1

2

From what I what is can see movimiento_det is a single object. Each iteration only updates that object. You need to create a new object over each iteration and modify that object.

foreach (var detalle in detalles)
{
    var movimiento_det = new MovimientoDet();
    movimiento_det.MovimientoDetId =   G.Serial("movimiento_det"); 
    movimiento_det.MovimientoDetId= i;
    movimiento_det.ProductoId = detalle.Value.ProductoId;
    movimiento_det.Cantidad = detalle.Value.Cantidad;
    Vmsb.MovimientoDets.Add(movimiento_det);
}
Vmsb.SaveChanges();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.