EF4误差:两个对象之间的关系不能被限定,因为它们连接到不同的ObjectContext对象 [英] EF4 error:The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects

查看:311
本文介绍了EF4误差:两个对象之间的关系不能被限定,因为它们连接到不同的ObjectContext对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有我'使用在使用德模型MVP(模型,视图,主持人)VS2010我的网站WSCF的问题和我的模型层(数据存取权限层)IAM使用EF



所以我有这个模型 http://yfrog.com/mymodelyj



这seguimiento的表是BE cliente和gventa表
,所以我有我的seguimiento与L2E表中插入我的(DAL LAYER)这样<之间的中间表/ p>

 公共无效InsertarSeguimiento(Seguimiento赛格)
{
使用(VAR续=新CelumarketingEntities())
{
cont.AddToSeguimiento(SEG);
cont.SaveChanges();
}
}

和我的演示文稿的图层,我抓住了我的网站形式,文本框为seguimiento领域,我得到这些错误,当我尝试把对象cliente到(seguimiento)objProxy.ClienteReference.Value结果
无法定义的两个对象之间的关系,因为它们连接到不同的ObjectContext对象。
,我不明白为什么,因为gventa对象没有该错误

 保护无效BtnInsertar_Click(对象发件人,EventArgs五)
{
串nombreGVentas = TbxVendedor.Text;
的char [] =划定新的char [] {''};
的String [] = arreglo nombreGVentas.Split(划);
GVenta IdGVentas = _presenter.getventas(arreglo [0],arreglo [1]);

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

串霍拉= DdlHora.SelectedValue;
串minutos = DdlMinutos.SelectedValue;

串HorMin =霍拉+:+ minutos;
Project.CAD.Seguimiento objProxy =新Project.CAD.Seguimiento();

objProxy.GVentaReference.Value = IdGVentas;
objProxy.ClienteReference.Value = idCliente; * //这里我得到的错误*
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()方法之前,从它的上下文脱离idCliente。


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

so I have this model http://yfrog.com/mymodelyj

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

这篇关于EF4误差:两个对象之间的关系不能被限定,因为它们连接到不同的ObjectContext对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆