English 中文(简体)
EF4错误:无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象
原标题: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

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);   
        }
最佳回答

问题是您的idCliente已附加到此处的上下文:

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

因此,当你试图将它分配给同样在其他上下文中的另一个对象(你得到错误的那一行)时,EF会抛出错误,因为它不知道将什么对象放在什么上下文中(它只能属于一个上下文)。

您需要做的是在返回_presenter.getCliente()方法之前,将idCliente从其上下文中分离出来。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签