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

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

问题描述

我读过一些问题/这都与此特定错误消息的答案,但我不太了解合适的解决方案。

我看过很多次,你应该创建EF4情况下,使用它,然后对其进行处理。在我的应用程序,我加载实体在这里和那里使用不同的上下文对象,然后最终想要的实体联想到一起。

我已经创建了一个简单的控制台应用程序,很容易导致错误。是图解其次是code的非常简单的模型。

我怎样才能获得两个不同的实体共享相同的背景下?难道我真的要创建一个新的背景下,再次装入两个实体(即使我已经有了他们),只需将它们联系起来,并保存?

如果我只是错过了一个已经存在的,适当的提问/回答,请点我在正确的地方。

 内部类节目{

    私有静态无效的主要(字串[] args){
        DeleteAllEntities();
        CreateInitialEntities();
        业主O = LoadOwner();
        儿童C = LoadChild();
        AssociateAndSave(O,C);
    }

    私有静态无效AssociateAndSave(所有者0,Child C级){
        使用(VAR上下文=新ModelEntities()){
            //异常发生在下面一行。
            o.Children.Add(C);
            context.Attach(O);
            context.SaveChanges();
        }
    }

    私有静态所有者LoadOwner(){
        使用(VAR上下文=新ModelEntities()){
            返回((邻在context.Owners
                     选择o)。首先());
        }
    }

    私有静态儿童LoadChild(){
        使用(VAR上下文=新ModelEntities()){
            返回((从C在context.Children
                     选择C)。首先());
        }
    }

    私有静态无效CreateInitialEntities(){
        使用(VAR上下文=新ModelEntities()){
            所有者所有者=新用户();
            孩童=新的儿童();
            context.Owners.AddObject(业主);
            context.Children.AddObject(子);
            context.SaveChanges();
        }
    }

    私有静态无效DeleteAllEntities(){
        使用(VAR上下文=新ModelEntities()){
            名单<儿童>孩子=(从C在context.Children
                                    选择C).ToList();
            的foreach(儿童变种C)
                context.Children.DeleteObject(C);
            名单<所有者>业主=(邻在context.Owners
                                  选择o).ToList();
            的foreach(所有者变种O)
                context.Owners.DeleteObject(O);
            context.SaveChanges();
        }
    }
}
 

解决方案

您应该得到一个参考的孩子,在同一背景下的所有者对象。对于这样的一个方法是获得ID,然后它们作为参数传递给 AssociateAndSave(INT OID,诠释CID)方法。

然后,您检索参考,在同样的情况下对象的 并您进行连接。

 私有静态无效的主要(字串[] args)
{
    DeleteAllEntities();
    CreateInitialEntities();
    INT OID = LoadOwnerId();
    INT CID = LoadChildId();
    AssociateAndSave(OID,CID);
}

私有静态诠释LoadOwnerId()
{
    使用(VAR上下文=新ModelEntities())
    {
        返回(从o在context.Owners
                选择o)。首先()标识。
    }
}

私有静态诠释LoadChildId()
{
    使用(VAR上下文=新ModelEntities())
    {
        回报(从C在context.Children
                选择C)。首先()标识。
    }
}

私有静态无效AssociateAndSave(INT OID,INT CID)
{
    使用(VAR上下文=新ModelEntities())
    {
        VAR所有者=(邻在context.Owners
                        选择o).FirstOrDefault(O => o.ID == OID);
        VAR的孩子=(邻在context.Children
                        选择o).FirstOrDefault(C => c.ID == CID);

        owner.Children.Add(子);
        context.Attach(业主);
        context.SaveChanges();
    }
}
 

I've read some questions/answers that have to do with this particular error message, but I'm not quite understanding the appropriate solution.

I've read many times that you should create the EF4 context, use it, then dispose of it. Throughout my application, I'm loading entities here and there using different context objects, and then eventually want to associate the entities together.

I've create a simple console application that easily causes the error. The very simple model is diagrammed followed by the code.

How can I get the two different entities to share the same context? Do I really have to create a new context, load the two entities again (even though I already have them), simply to associate them and save?

If I simply missed an already existing, appropriate question/answer, please point me to the right place.

internal class Program {

    private static void Main(string[] args) {
        DeleteAllEntities();
        CreateInitialEntities();
        Owner o = LoadOwner();
        Child c = LoadChild();
        AssociateAndSave(o, c);
    }

    private static void AssociateAndSave(Owner o, Child c) {
        using (var context = new ModelEntities()) {
            // Exception occurs on the following line.
            o.Children.Add(c);
            context.Attach(o);
            context.SaveChanges();
        }
    }

    private static Owner LoadOwner() {
        using (var context = new ModelEntities()) {
            return ((from o in context.Owners
                     select o).First());
        }
    }

    private static Child LoadChild() {
        using (var context = new ModelEntities()) {
            return ((from c in context.Children
                     select c).First());
        }
    }

    private static void CreateInitialEntities() {
        using (var context = new ModelEntities()) {
            Owner owner = new Owner();
            Child child = new Child();
            context.Owners.AddObject(owner);
            context.Children.AddObject(child);
            context.SaveChanges();
        }
    }

    private static void DeleteAllEntities() {
        using (var context = new ModelEntities()) {
            List<Child> children = (from c in context.Children
                                    select c).ToList();
            foreach (var c in children)
                context.Children.DeleteObject(c);
            List<Owner> owners = (from o in context.Owners
                                  select o).ToList();
            foreach (var o in owners)
                context.Owners.DeleteObject(o);
            context.SaveChanges();
        }
    }
}

解决方案

You should get a reference to the child and owner objects in the same context. An approach for this would be to get the ids and then pass them as parameters to the AssociateAndSave(int oId, int cId) method.

Then, you retrieve the references to the objects in the same context and you make the attachment.

private static void Main(string[] args)
{
    DeleteAllEntities();
    CreateInitialEntities();
    int oId = LoadOwnerId();
    int cId = LoadChildId();
    AssociateAndSave(oId, cId);
}

private static int LoadOwnerId()
{
    using (var context = new ModelEntities())
    {
        return (from o in context.Owners
                select o).First().Id;
    }
}

private static int LoadChildId()
{
    using (var context = new ModelEntities())
    {
        return (from c in context.Children
                select c).First().Id;
    }
}

private static void AssociateAndSave(int oId, int cId)
{
    using (var context = new ModelEntities())
    {
        var owner = (from o in context.Owners
                        select o).FirstOrDefault(o => o.ID == oId);
        var child = (from o in context.Children
                        select o).FirstOrDefault(c => c.ID == cId);

        owner.Children.Add(child);
        context.Attach(owner);
        context.SaveChanges();
    }
}

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

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