Entity framework 实体框架更新主详细信息项时出错
大家好,我正在尝试将新项目添加到主详细信息记录中,我发现错误: INSERT语句与外键约束“invOrden\U InvOrdenDet\U FK1”冲突。冲突发生在数据库“InventarioSIAIplus”、表“dbo.InvOrden”、列“IDorden”中。 声明已终止 将新项目添加到详细信息时会发生此错误 谢谢你的帮助。 我用于添加和更新数据的代码: InventarioSIAIplusEntities SIAplusContext=(InventarioSIAIplusEntities)(会话[“上下文]); 伊诺登·奥登Entity framework 实体框架更新主详细信息项时出错,entity-framework,Entity Framework,大家好,我正在尝试将新项目添加到主详细信息记录中,我发现错误: INSERT语句与外键约束“invOrden\U InvOrdenDet\U FK1”冲突。冲突发生在数据库“InventarioSIAIplus”、表“dbo.InvOrden”、列“IDorden”中。 声明已终止 将新项目添加到详细信息时会发生此错误 谢谢你的帮助。 我用于添加和更新数据的代码: InventarioSIAIplusEntities SIAplusContext=(InventarioSIAIplusEntit
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();
用于将临时项目添加到详图的代码
InventarioSIAIplusEntities SIAplusContext=(InventarioSIAIplusEntities)(会话[“上下文]);
列表药物=详细药物顺序
//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();
该错误基本上意味着您试图在没有从主表提供合法FK的情况下向详细信息表添加一行。
我猜这是在执行if代码块(txtIDorden.Text.Trim()==“”)时发生的,这意味着您正在处理一个全新的orden对象,该对象在保存之前没有任何EntityKey。我可以看出,您正试图通过InvOrdenReference将detail对象显式设置为master,这在您的场景中是没有意义的。我的建议是通过navigation属性设置它,并让EF在运行时为您修复它。所以你的foreach应该是这样的:
foreach (var item in DetalleMedicamentosOrden) {
if (item.InvOrdenReference.Value == null) {
item.InvOrden = orden;
}
}
嘿,谢谢你的回答,这是有道理的,但当我把代码的方式,你说,我仍然有一个错误,当我试图添加和项目的细节。错误“ObjectStateManager中已存在具有相同键的对象”。但该对象是新对象。谢谢你的帮助,没问题。您使用的是哪个EF版本?您还可以在ordenRepository.Add()和efUnitOfWork.Save()方法中发布代码吗?您好,我使用的是entity framework 4和一个从codeplex下载的模板,该模板实现了存储库模式。public void Add(InvOrden entity){repository.Add(entity);}public efUnitOfWork(){Context=new InventarioSIAIplusEntities();}公共void Save(){Context.SaveChanges();}