2010-09-30 39 views
0

大家好我嘗試添加新的項目,掌握詳細記錄,我得到的錯誤:實體框架錯誤更新主明細情況項目

INSERT語句衝突與外鍵約束「invOrden_InvOrdenDet_FK1」。衝突發生於數據庫「InventarioSIAIplus」,表「dbo.InvOrden」,列「IDorden」。 該聲明已被終止。

這個錯誤發生在我添加一個新的項目細節。

感謝您的任何幫助。 CODE我使用的用於添加和更新數據: InventarioSIAIplusEntities SIAplusContext =(InventarioSIAIplusEntities)(會話[ 「上下文」]); InvOrden orden;

 if (txtIDorden.Text.Trim() == "") 
     { 
      orden = new InvOrden(); 
      orden.IDcentro = Convert.ToInt32(ddlCentros.SelectedValue); 
      orden.estado = ddlEstadoOrden.SelectedValue; 
      orden.fecha = DateTime.Now; 
      orden.comentario = txtComentarioOrden.Text; 
      orden.usuarioCrea = "Jeanc";     
      SIAplusContext.AttachTo("InvOrden",orden);    
     } 
     else 
     { 
      int idorden = Convert.ToInt32(txtIDorden.Text.Trim()); 
      orden = SIAplusContext.InvOrdenes.Where(c => c.IDorden == idorden).First(); 
       //orden.lo.getOrden());     
      orden.estado = ddlEstadoOrden.SelectedValue; 
      orden.fecha = DateTime.Now; 
      orden.comentario = txtComentarioOrden.Text; 
      orden.usuarioCrea = "Jeanc"; 

     } 

     foreach (var item in DetalleMedicamentosOrden) 
     { 
      if (item.InvOrdenReference.Value == null) 
      { 
       item.InvOrden = orden; 

      } 
     } 

     SIAplusContext.SaveChanges(); 

CODE添加條目TEMPORARY到細節

InventarioSIAIplusEntities SIAplusContext =(InventarioSIAIplusEntities)(會話[ 「上下文」]); 列表吃藥= DetalleMedicamentosOrden;

  //Datos Detalle 
     InvOrdenDet ordenDetalle = new InvOrdenDet();    
     ordenDetalle.cantidadSol = uscAgregarMedicamentos1.Cantidad; 
     ordenDetalle.cantidadApr = uscAgregarMedicamentos1.CantidadAprobada; 

     ordenDetalle.comentario=uscAgregarMedicamentos1.Comentario; 
     ordenDetalle.comentario = uscAgregarMedicamentos1.Comentario; 
     ordenDetalle.IDmedicamento=uscAgregarMedicamentos1.IDmedicamento; 

     //Agrego el detalle a la lista de detalles que se va guardando en la memoria. 
     meds.Add(ordenDetalle); 

     //Paso la lista con el nuevo objecto actualizada. 
     DetalleMedicamentosOrden = meds; 

     //Consulto la lista con para hacer una proyeccion del query y trae el nombre del medicamento 
     var medInfo = from a in DetalleMedicamentosOrden       
        select new { a, a.cantidadSol, a.cantidadApr }; 

     //Cargo la data en el gridview. 
     gvMedicamentosOrden.DataSource = medInfo; 
     gvMedicamentosOrden.DataBind(); 

     //Mando a mostrar 
     mpePnMedicamentos.Show(); 

回答

0

該錯誤基本上意味着您正試圖向細節表添加一行而不從主表提供合法的FK。
我猜這個時候你如果代碼(txtIDorden.Text.Trim()==「」)正在執行,這意味着你正在處理一個全新奧登對象,沒有任何情況發生的EntityKey保存之前。我可以看到你正在試圖通過InvOrdenReference這沒有意義在方案中明確設置明細對象的主人。我建議通過導航屬性來設置它,並讓EF在運行時爲您解決它。所以,你的的foreach應該是這樣的:

foreach (var item in DetalleMedicamentosOrden) { 
    if (item.InvOrdenReference.Value == null) { 
     item.InvOrden = orden;     
    } 
} 
+0

嘿感謝您的回答是有意義,但是當我把代碼的方式,你說,我仍然有一個錯誤,當我嘗試添加和項目到細節。錯誤「ObjectStateManager中已存在具有相同鍵的對象。」但該對象是一個新的。謝謝你的幫助。 – 2010-10-01 13:05:26

+0

沒問題。你使用哪個EF版本?你也可以發佈ordenRepository.Add()和efUnitOfWork.Save()方法中的代碼嗎? – 2010-10-01 14:27:34

+0

嗨,我使用實體框架4與我從實現存儲庫模式的codeplex下載的模板。 – 2010-10-02 15:13:41