使用现有对象插入新对象 [英] Insert new object with existing object

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

问题描述




  • 根据我的数据库创建一个edmx文件

    我是EF 4的新手, / li>

  • 为我的对象(PO​​CO)创建代码生成。现在我有一个model1.tt,当我扩展我看到我的类

  • 根据IRepository

$ b为每个类创建一个存储库
$ b

现在,我正在使用两个对象A和B.对象A有一个类型B的属性。在我的winform中,我有一个包含类型B的对象的组合。当保存按钮是按下,创建A类的新实例,并设置所有属性。对象B属性设置如下:

  objectA.myObjectB =(objectB)cmbBObjects.selectedItem; 

然后我创建一个objectA的存储库并调用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();
返回对象;
}
}

在我的表单中:

  ObjectBRepository rep = new ObjectBRepository(); 
IList< ObjectB> objects = rep.GetAll;

cmbBObjects.Datasource = objects;
// etc ..

所以我的问题是,我该怎么做保存对象A而不创建objectB的新记录?

解决方案

如果不想插入 objectB 您必须通知EF。当您为 objectA 调用 context.objectAs.AddObject(obj)时,您说:我要插入objectA及其所有依赖项。但是显然你不想保存依赖,所以你必须:




  • 加载 objectB 从DB添加到 objectA 之前。在这种情况下,EF会知道 objectB 是现有对象,它不会再插入。

  • 附加 objectB 添加到 objectA 之前的上下文。 ObjectB 将被处理为现有但不变。

  • 设置状态 objectB 插入 objectA 后。您可以通过调用以下方法: context.ObjectStateManager.ChangeObjectState(objectB,EntityState.Unchanged)



拳头建议示例:

  var id = objectB.Id; 
objectA.myObjectB = context.ObjectBs.SingleOrDefault(o => o.Id == id);

第二个建议的示例:

  context.ObjectBs.Attach(对象B); 
objectA.myObjectB = objectB;

第三个建议的示例:

  objectA.myObjectB = objectB; 
context.ObjectAs.AddObject(objectA);
context.ObjectStateManager.ChangeObjectState(objectB,EntityState.Unchanged);


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?

解决方案

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:

  • Load objectB from DB before adding it to objectA. In such case EF will know that objectB is existing object and it will not insert it again.
  • Attach objectB to context before adding it to objectA. ObjectB will be handled as existing but unchanged.
  • Set the state of objectB after inserting objectA. You can do that by calling: context.ObjectStateManager.ChangeObjectState(objectB, EntityState.Unchanged)

Example of the fist suggestion:

var id = objectB.Id;
objectA.myObjectB = context.ObjectBs.SingleOrDefault(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屋!

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