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

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

问题描述

我已经阅读了一些与这个特定错误信息有关的问题/答案,但我不太了解适当的解决方案。

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

阅读多次,你应该创建EF4上下文,使用它,然后处理它。在我的应用程序中,我使用不同的上下文对象加载实体,然后最终将实体关联在一起。

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();
        }
    }
}


推荐答案

您应该在同一上下文中引用子对象和所有者对象。一种方法是获取ids,然后将它们作为参数传递给 AssociateAndSave(int oId,int cId)方法。

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天全站免登陆