附加现有的对象图与新的子对象问题... [英] Attach existing Object Graph with new subobjects problem...

查看:66
本文介绍了附加现有的对象图与新的子对象问题...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在C#Windows应用程序中使用Entity Framework 4。我有两个课程项目和产品。 Project有一个Product的属性集合。这是我的数据库中的1对多关系。我的应用程序是N-Tiered,因此在加载数据库中的对象之后,它们与上下文分离。因此,如果我想再次保存它们,我必须将对象重新附加到上下文中。

如果我有一个新项目,可以使用AddObject方法将其添加到上下文中。如果Project已经存在,那么我使用Attach方法将其附加到上下文。我注意到,如果父对象被附加,所有子对象(在我的情况下是项目中的产品集合)也附加到上下文。

如果我想附加现有项目,我的问题就开始了两个以上的新产品。因为Project存在于db中,所以我使用attach方法(而不是AddObject),但是然后实体框架也试图附加两个产品(就好像它们也存在一样),这引发了一个异常,说现有两个对象相同的ID。

如何说EF,附加父对象,但添加两个子对象?

希望有人可以提供帮助,问候......


F / \\\
t / \\\ PRODUCTIONS

Hi everyone,

I am using Entity Framework 4 in my C# Windows application. I have two classes Project and Product. Project has a collection of Product's as property. This is a 1-To-Many relationship in my database. My application is N-Tiered, so after objects from database are load, they got detached from context. So if I want to save them again, I have to reattach the objects to the context.

If I have a new Project, its easy to add it to the context using the AddObject Method. If Project were already existing, then I attach it to the context using the Attach method. I noticed, that all subobjects (in my case the collection of Products in Project) are also attached to context, if the parent object is attached.

My problem begins if I want to attach an existing Project with two ore more NEW Products. Because Project is existing in db, I use the attach method (instead of AddObject), But then Entity Framework tries to also attach the two Products (as if they were also existing) and this throws an exception saying, there are existing two objects with the same ID.

How can I say EF, attach the parent object, but ADD the two subobjects?

Hope someone can help,
regards...


F/\nt/\sy PRODUCTIONS

推荐答案

你好,所以仍然没有答案...任何人都可以告诉我,如果我是唯一遇到此问题的人,或者这是一个常见问题吗?
Hi there,

so still no answer... Can anyone tell me, if I am the only one experiencing this problem, or is this a common issue?


这篇关于附加现有的对象图与新的子对象问题...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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