[英]'The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.' - Error
[英]EF4 error:The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects
嗨我有一個問題我在vs2010中使用我的網站wscf使用de model MVP(模型,視圖,演示者)和我的模型層(數據訪問層)使用EF
seguimiento的表是cliente和gventa表之間的中間表,所以我在seguimiento的表中插入了我的(DAL LAYER)中的L2E,就像這樣
public void InsertarSeguimiento(Seguimiento Seg)
{
using (var cont = new CelumarketingEntities())
{
cont.AddToSeguimiento(Seg);
cont.SaveChanges();
}
}
在我的演示文稿'S層中,我捕獲我的web表單,從文本框中搜索seguimiento的字段當我嘗試將對象cliente放到(seguimiento)objProxy.ClienteReference.Value時我得到這些錯誤
無法定義兩個對象之間的關系,因為它們附加到不同的ObjectContext對象。 我不明白為什么因為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);
}
問題是您的idCliente已經附加到上下文中:
Project.CAD.Cliente idCliente = _presenter.getCliente(nombrecliente);
因此,當您嘗試將其分配給另一個也在其他上下文中的對象(您收到錯誤的行)時,EF會拋出錯誤,因為它不知道要放在什么上下文中的對象(它可以屬於只有一個背景)。
您需要做的是在返回_presenter.getCliente()
方法之前_presenter.getCliente()
上下文中分離idCliente。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.