将现有对象新对象 [英] Insert new object with existing object
问题描述
我是新来的EF 4,这是我迄今所做的:
I am new to EF 4 and this is what I have done so far:
- 创建一个EDMX文件的基础上我的数据库
- 创建一个code代我对象(POCO)。现在我有一个model1.tt,在扩大我看到人我的班
- 创建一个存储库中为每个类的基础上,IRepository
现在,我有两个对象,A和B.对象A的工作具有类型B的属性。在我的winform我充满了型B.当保存按钮的对象组合为pressed,一A类的新实例被创建,所有的属性设置。对象B属性被设置如下:
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;
然后,我创建对象A的存储库,并调用保存方法。在这种保存方法,我有这个code±
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();
}
}
这code,不保存新的条目到数据库中,但它也创造了对象B的新纪录!我不希望这样,因为对象B已经存在于数据库中! (我选择了这一个从组合框)。
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:
在对象B库:
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,而无需创建一个新的记录对象B?
So my question is, what do I have to do to save object A without creating a new record for objectB?
推荐答案
如果你不想插入对象B
您必须通知EF它。当你调用 context.objectAs.AddObject(OBJ)
为对象A
你说的话:我要插入对象A和它的所有依赖的。但显然你不希望保存依赖条件,所以你必须:
If you don't want insert objectB
you must inform EF about it. When you call context.objectAs.AddObject(obj)
for objectA
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:
- 加载
对象B
从数据库将其添加到对象A
之前。在这种情况下,EF就会知道,对象B
是现有对象,它不会再插入。 - 附加
对象B
上下文将其添加到对象A
之前。对象B
将作为现有的,但不变的处理。 - 设置在插入后的
对象B
状态对象A
。你可以通过调用:context.ObjectStateManager.ChangeObjectState(对象B,EntityState.Unchanged)
- Load
objectB
from DB before adding it toobjectA
. In such case EF will know thatobjectB
is existing object and it will not insert it again. - Attach
objectB
to context before adding it toobjectA
.ObjectB
will be handled as existing but unchanged. - Set the state of
objectB
after insertingobjectA
. You can do that by calling:context.ObjectStateManager.ChangeObjectState(objectB, EntityState.Unchanged)
拳头建议的例子:
var id = objectB.Id;
objectA.myObjectB = context.ObjectBs.SingleOrDefautl(o => o.Id == id);
第二项建议的例子:
Example of the second suggestion:
context.ObjectBs.Attach(objectB);
objectA.myObjectB = objectB;
第三个建议的例子:
Example of the third suggestion:
objectA.myObjectB = objectB;
context.ObjectAs.AddObject(objectA);
context.ObjectStateManager.ChangeObjectState(objectB, EntityState.Unchanged);
这篇关于将现有对象新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!