开发者

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

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

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

The relation开发者_Go百科ship between the two objects cannot be defined because they are attached to different ObjectContext objects. and I don't understand why since gventa object have no that error

 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:

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).

What you need to do is to detach idCliente from it's context before returning in _presenter.getCliente() method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜