简体   繁体   English

EF4错误:无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象

[英]EF4 error:The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects

hi I have a question I'am using for my web site wscf in vs2010 that use de model MVP(model,view,presenter) and for my model layer (data acces layer) iam using EF 嗨我有一个问题我在vs2010中使用我的网站wscf使用de model MVP(模型,视图,演示者)和我的模型层(数据访问层)使用EF

that seguimiento's tables is an intermediate table between be cliente and gventa tables so I have my Insert in seguimiento's table with L2E in my (DAL LAYER)like this seguimiento的表是cliente和gventa表之间的中间表,所以我在seguimiento的表中插入了我的(DAL LAYER)中的L2E,就像这样

public void InsertarSeguimiento(Seguimiento Seg)
    {
        using (var cont = new CelumarketingEntities())
        {
            cont.AddToSeguimiento(Seg);
            cont.SaveChanges();
        }
    }

and in my presentation'S layer, I capture for my web form, from textbox the field for seguimiento And I get these error when I try to put the object cliente to (seguimiento) objProxy.ClienteReference.Value 在我的演示文稿'S层中,我捕获我的web表单,从文本框中搜索seguimiento的字段当我尝试将对象cliente放到(seguimiento)objProxy.ClienteReference.Value时我得到这些错误
The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects. 无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象。 and I don't understand why since gventa object have no that error 我不明白为什么因为gventa对象没有那个错误

 protected void BtnInsertar_Click(object sender, EventArgs e)
        {
            string nombreGVentas = TbxVendedor.Text;
            char[] delimit = new char[] { ' ' };
            string[] arreglo = nombreGVentas.Split(delimit);
            GVenta IdGVentas = _presenter.getventas(arreglo[0], arreglo[1]);

            string nombrecliente = TbxCliente.Text;
            Project.CAD.Cliente  idCliente = _presenter.getCliente(nombrecliente);

            string hora = DdlHora.SelectedValue;
            string minutos = DdlMinutos.SelectedValue;

            string HorMin = hora + ":" + minutos;
            Project.CAD.Seguimiento objProxy = new Project.CAD.Seguimiento();

            objProxy.GVentaReference.Value = IdGVentas;
            objProxy.ClienteReference.Value = idCliente;   *// here i get the errors*
            objProxy.Descripccion = TbxDescripccion.Text;
            objProxy.Fecha = Calendar1.SelectedDate;
            objProxy.Hora = HorMin;

             _presenter.insertarseg(objProxy);   
        }

Problem is that your idCliente is already attached to the context here: 问题是您的idCliente已经附加到上下文中:

Project.CAD.Cliente  idCliente = _presenter.getCliente(nombrecliente);

So, when you try to assign it to the other object that's also in some other context (the line where you get the error), the EF throws error since it don't know what object to put in what context (it can belong to only one context). 因此,当您尝试将其分配给另一个也在其他上下文中的对象(您收到错误的行)时,EF会抛出错误,因为它不知道要放在什么上下文中的对象(它可以属于只有一个背景)。

What you need to do is to detach idCliente from it's context before returning in _presenter.getCliente() method. 您需要做的是在返回_presenter.getCliente()方法之前_presenter.getCliente()上下文中分离idCliente。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “无法定义两个对象之间的关系,因为它们被附加到不同的ObjectContext对象。” -错误 - 'The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.' - Error 无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象Entity Framework - The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects Entity Framework 无法定义两个对象之间的关系,因为它们附加到不同的 ObjectContext 对象 - The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects 引发异常:因为两个对象附​​加到不同的ObjectContext对象,所以无法定义它们之间的关系 - Exception thrown: The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects 实体框架v4 - 无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象 - Entity Framework v4 - The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects 无法定义两个对象之间的关系,因为它们已附加到不同的ObjectContext对象 - The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects 实体框架:“无法定义两个对象之间的关系,因为它们附加到不同的 ObjectContext 对象。” - Entity Framework: “The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.” 无法定义关系,因为它们已附加到不同的ObjectContext对象 - Relationship cannot be defined because they are attached to different ObjectContext objects 无法找出哪些两个对象附​​加到不同的ObjectContext对象 - Cannot figure out which two objects are attached to different ObjectContext objects 无法定义两个对象之间的关系-为什么? - The relationship between the two objects cannot be defined - why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM