EF4 error: the relationship between two objects cannot be determined because they are bound to different ObjectContext objects

Hi, I have a question that I use for my WSCF website in vs2010, which use the MVP model (model, view, presenter) and for my model level (data access layer) IAM using EF

that seguimiento tables are an intermediate table between be cliente and gventa tables, so I have my Insert in seguimiento 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 at my presentation level I am writing a field for seguimiento for my web form from a text box. And I get these errors when I try to put the object client in (seguimiento) objProxy.ClienteReference.Value.
The relationship between two objects cannot be determined because they are attached to different ObjectContext objects. and I don’t understand why, since the Gventa object does not have this 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); } 
+4
source share
1 answer

The problem is that your idCliente is already context bound here:

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

So, when you try to assign it to another object that is also in a different context (the line where you get the error), EF throws an error, because it does not know which object to put in which context (this can belong to only one context).

What you need to do is to disable idCliente from the context before returning to the _presenter.getCliente() method.

+5
source

All Articles