views:

24

answers:

1

Hi everybody im starting to work with Entity Framework 4.0 and ASP.NET 4.0 i'm trying to make a master detail web page and i'm having problems when i try to add new items to a previously recorded items below is the i made for this:

private void guardarOrdenMedicamento()
    {

        InventarioSIAIplusEntities SIAplusContext = (InventarioSIAIplusEntities)(Session["context"]);

        InvOrden orden;
        string resultMessage;

        if (DetalleMedicamentosOrden.Count == 0)

        {
            MessageBox1.ShowError("Debe especificar al menos un medicamento para la orden.");
            return;

        }

        if (txtIDorden.Text.Trim() == "")
        {
             orden = new InvOrden();

             orden.IDcentro = Convert.ToInt32(ddlCentros.SelectedValue);
             orden.estado = ddlEstadoOrden.SelectedValue;
             orden.fecha = Convert.ToDateTime(txtFechaCreacion.Text);

             orden.comentario = txtComentarioOrden.Text;
             orden.usuarioCrea = "Jeanc";                
             SIAplusContext.AddToInvOrdenes(orden);

             resultMessage = "La orden fue registrada satisfactoriamente";

        }
        else

        {
             int idorden = Convert.ToInt32(txtIDorden.Text.Trim());
             orden = SIAplusContext.InvOrdenes.Where(c => c.IDorden == idorden).First();      

             orden.estado = ddlEstadoOrden.SelectedValue;
             orden.fecha = Convert.ToDateTime(txtFechaCreacion.Text);
             orden.comentario = txtComentarioOrden.Text;

             orden.usuarioCrea = "Jeanc";
             resultMessage = "La orden fue actualizada satisfactoriamente";


             foreach (var item in DetalleMedicamentosOrden)
             {
                 if (item.IDorden == 0)

                 {
                // item.IDorden = idorden;
                //     item.InvOrdenReference.EntityKey =  orden.EntityKey;

                         //neww System.Data.EntityKey("InventarioSIAIplusEntities.InvOrdenes", "IDorden", orden.IDorden);
                //   SIAplusContext.AddToInvOrdenDets(item);

                  item.InvOrden = orden;
                   //
              // SIAplusContext.AddToInvOrdenDets(item);
                 //orden.InvOrdenDets.Add(item);

                 }
                 else
                 {
                     InvOrdenDet det = SIAplusContext.InvOrdenDets.Where(c => c.IDorden == item.IDorden && c.IDmedicamento == item.IDmedicamento).First();

                     det.cantidadApr = item.cantidadApr;
                     det.cantidadSol = item.cantidadSol;
                     det.comentario = item.comentario;
                 }
             } 

        }


        SIAplusContext.SaveChanges();

   }

The Error is :The object could not be added or attached because its EntityReference has an EntityKey property value that does not match the EntityKey for this object. Thanks For any help with this.

A: 

I think that the problem is here:

         item.InvOrden = orden;

You either have to use AddObject or update the values of an existing object.

Shiraz Bhaiji
I've tried both and i get an error too
Jean