将新对象插入现有对象
我是 EF 4 的新手,这是我到目前为止所做的:
- 根据我的数据库创建 edmx 文件
- 为我的对象 (POCO) 创建代码生成。现在我有一个 model1.tt,展开后我会看到我的所有类
- 基于 IRepository 为每个类创建一个存储库
现在,我正在使用两个对象 A 和 B。对象 A 具有类型 B 的属性。在我的 winform 中我有一个充满 B 类型对象的组合。按下保存按钮时,将创建 A 类的新实例并设置所有属性。对象 B 属性设置如下:
objectA.myObjectB = (objectB)cmbBObjects.selectedItem;
然后我为对象 A 创建一个存储库并调用 save 方法。在这个保存方法中,我有这个代码±
public bool Save(ObjectA obj)
{
using(MyContext context = new MyContext())
{
context.objectAs.AddObject(obj);
context.SaveChanges();
}
}
这个代码,确实将一个新条目保存到数据库中,但它也为对象 B 创建了一个新记录!我不想要这个,因为对象 B 已经存在于数据库中! (我从组合框中选择了这个)。
这就是我填充组合框的方式:
在 objectB 存储库中:
public IList<ObjectB> GetAll()
{
using(MyContext context = new MyContext())
{
IList<ObjectB> objects = context.objectBs.ToList();
return objects;
}
}
在我的表单中:
ObjectBRepository rep = new ObjectBRepository();
IList<ObjectB> objects = rep.GetAll;
cmbBObjects.Datasource = objects;
// etc..
所以我的问题是,我必须做什么才能保存对象 A 而不为 objectB 创建新记录?
I am new to EF 4 and this is what I have done so far:
- Create an edmx file based on my database
- Create a code generation for my objects (POCO). Now I have a model1.tt, when expanded I see al my classes
- Create a repository for each class, based on IRepository
Now, I am working with two objects, A and B. Object A has a property of type B. In my winform I have a combo filled with objects of type B. When the save button is pressed, a new instance of class A is created and all the properties are set. The object B property is set as follows:
objectA.myObjectB = (objectB)cmbBObjects.selectedItem;
Then I create a repository for objectA and call the save method. In this save method I have this code±
public bool Save(ObjectA obj)
{
using(MyContext context = new MyContext())
{
context.objectAs.AddObject(obj);
context.SaveChanges();
}
}
This code, does save a new entry to the database, but it is also creating a new record for object B! I don't want this, because object B already exists in the database! (I have selected this one from the combobox).
This is how I fill my combobox:
In the objectB repository:
public IList<ObjectB> GetAll()
{
using(MyContext context = new MyContext())
{
IList<ObjectB> objects = context.objectBs.ToList();
return objects;
}
}
In my form:
ObjectBRepository rep = new ObjectBRepository();
IList<ObjectB> objects = rep.GetAll;
cmbBObjects.Datasource = objects;
// etc..
So my question is, what do I have to do to save object A without creating a new record for objectB?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不想插入
objectB
,则必须通知 EF。当您为objectA
调用context.objectAs.AddObject(obj)
时,您是在说:我想插入 objectA 及其所有依赖项。但显然您不想保存依赖项,因此您必须:objectB
,然后将其添加到objectA
。在这种情况下,EF 将知道objectB
是现有对象,并且不会再次插入它。objectB
添加到objectA
之前,将其附加到上下文。ObjectB
将被视为现有但未更改。objectA
后设置objectB
的状态。您可以通过调用:context.ObjectStateManager.ChangeObjectState(objectB, EntityState.Unchanged)
第一个建议示例:
第二个建议示例:
第三个建议示例:
If you don't want insert
objectB
you must inform EF about it. When you callcontext.objectAs.AddObject(obj)
forobjectA
you are saying: I want to insert objectA and all its dependencies. But obviously you don't want to save dependecies so you must either:objectB
from DB before adding it toobjectA
. In such case EF will know thatobjectB
is existing object and it will not insert it again.objectB
to context before adding it toobjectA
.ObjectB
will be handled as existing but unchanged.objectB
after insertingobjectA
. You can do that by calling:context.ObjectStateManager.ChangeObjectState(objectB, EntityState.Unchanged)
Example of the fist suggestion:
Example of the second suggestion:
Example of the third suggestion:
我认为该问题出现在以下行中:
由于结果
(objectB)cmbBObjects.selectedItem
与 datacontext 实体框架分离创建新实例。相反,您可以:1.将 objectB id 分配给 objectA
2.或者从 dataContext 加载 objectB 然后分配给 objectA:
只需尝试我的建议并返回结果,因为我不确切知道。
希望这有帮助。
I suppose that problem in following row:
Because of result
(objectB)cmbBObjects.selectedItem
detached from datacontext entity framework create new instance. Instead this you can:1.Assign objectB id to objectA
2.Or load objectB from dataContext and than assign to objectA:
Just try my suggestions and come back with results, because i don't know exactly.
Hope this help.