实体框架4 - ADDOBJECT VS附加 [英] Entity Framework 4 - AddObject vs Attach

查看:161
本文介绍了实体框架4 - ADDOBJECT VS附加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在与实体框架4最近,和我略有困惑,何时使用对象集。附加 ObjectSet.AddObject

I have been working with Entity Framework 4 recently, and am slightly confused as to when to use ObjectSet.Attach, and ObjectSet.AddObject.

这是我的理解:

  • 使用附加当一个实体已经存在于系统中
  • 使用ADDOBJECT创造一个品牌,当新的实体

所以,如果我是创建一个新的Person ,我这样做。

So, if i'm creating a new Person, i do this.

var ctx = new MyEntities();
var newPerson = new Person { Name = "Joe Bloggs" };
ctx.Persons.AddObject(newPerson);
ctx.SaveChanges();

如果我是修改现有的人,我这样做:

var ctx = new MyEntities();
var existingPerson = ctx.Persons.SingleOrDefault(p => p.Name = "Joe Bloggs" };
existingPerson.Name = "Joe Briggs";
ctx.SaveChanges();

请记住,这是一个很简单的例子。在现实中我使用纯POCO的(没有code代),库模式(不涉及ctx.Persons),以及工作单位(不涉及ctx.SaveChanges)。但是,在幕后,以上是我的实现会发生什么。

Keep in mind, this is a very simple example. In reality i am using Pure POCO's (no code generation), Repository pattern (don't deal with ctx.Persons), and Unit of Work (don't deal with ctx.SaveChanges). But "under the covers", the above is what happens in my implementation.

现在,我的问题 - 我还没有找到这样一个场景,我不得不使用连接

Now, my question - I am yet to find a scenario where i have had to use Attach.

我在想什么吗?我们什么时候需要使用附加?

What am i missing here? When do we need to use Attach?

修改

只是为了澄清,我正在寻找的例子的时候使用连接在ADDOBJECT(反之亦然)的。

Just to clarify, i'm looking for examples of when to use Attach over AddObject (or vice-versa).

编辑2

下面的答案是正确的(我接受),但认为我想补充另一个例子,连接将是有益的。

The below answer is correct (which i accepted), but thought i'd add another example where Attach would be useful.

在我上面的例子中的修改现有的人,两个查询实际上被执行。

In my above example for modifying an existing Person, two queries are actually being executed.

一检索人(.SingleOrDefault),另一个执行更新(.SaveChanges)。

One to retrieve the Person (.SingleOrDefault), and another to perform the UPDATE (.SaveChanges).

如果(出于某种原因),我已经知道了乔布洛格斯存在于系统中,为什么额外的查询,让他第一次?我能做到这一点:

If (for some reason), i already knew that "Joe Bloggs" existed in the system, why do an extra query to get him first? I could do this:

var ctx = new MyEntities();
var existingPerson = new Person { Name = "Joe Bloggs" };
ctx.Persons.Attach(existingPerson);
ctx.SaveChanges();

这将导致被执行的只是一个UPDATE语句。

This will result in just an UPDATE statement being executed.

推荐答案

<一个href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.addobject.aspx">ObjectContext.AddObject ObjectSet.AddObject
ADDOBJECT 的方法是将新创建的对象做的没有存在于数据库中。该实体将得到一个自动生成的临时的的EntityKey 的其 的EntityState将被设置为新增的。当调用SaveChanges被调用时,将清楚本实体需要被插入到数据库中的EF。

ObjectContext.AddObject and ObjectSet.AddObject:
The AddObject method is for adding newly created objects that do not exist in the database. The entity will get an automatically generated temporary EntityKey and its EntityState will be set to Added. When SaveChanges is called, it will be clear to the EF that this entity needs to be inserted into the database.

<一个href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.attach.aspx">ObjectContext.Attach ObjectSet.Attach
在另一方面,连接的用于实体已经的存在在数据库中。而不是设置 的EntityState来增加,在附加结果的的不变的的EntityState,这意味着它并没有改变,因为它是附加到上下文。您所连接的对象都假定在数据库中存在。如果修改了对象,他们已经连接后,当你调用的SaveChanges的的EntityKey值用于更新(或删除)相应的行通过查找其匹配的ID在数据库表。

此外,使用连接方法,你可以定义已经在ObjectContext中存在的实体之间的关系,但有没有被自动连接。基本上附加的主要目的,是连接那些已经连接到ObjectContext的和都是实体的没有的新的,所以你不能用附加附加实体的的EntityState添加。你必须使用的添加()的这种情况。

例如,假设您的Person实体有一个名为导航属性的地址的是地址的实体的集合。比方说,您已经阅读从上下文两个对象,但他们不互相关联,你想让它这样:

ObjectContext.Attach and ObjectSet.Attach:
On the other hand, Attach is used for entities that already exist in the database. Rather than setting the EntityState to Added, Attach results in an Unchanged EntityState, which means it has not changed since it was attached to the context. Objects that you are attaching are assumed to exist in the database. If you modify the objects after they’ve been attached, when you call SaveChanges the value of the EntityKey is used to update (or delete) the appropriate row by finding its matching ID in the db table.

Furthermore, using the Attach method, you can define relationships between entities that already exist in the ObjectContext but that have not been connected automatically. Basically the main purpose of Attach, is to connect entities that are already attached to the ObjectContext and are not new so you cannot use Attach to attach entities whose EntityState is Added. You have to use Add() in this case.

For example, let's assume your Person entity has a navigation property named Addresses which is a collection of Address entity. Let's say you have read both Objects from context, but they are not related to each other and you want to make it so:

var existingPerson = ctx.Persons.SingleOrDefault(p => p.Name = "Joe Bloggs" };
var myAddress = ctx.Addresses.First(a => a.PersonID != existingPerson.PersonID);
existingPerson.Addresses.Attach(myAddress);
// OR:
myAddress.PersonReference.Attach(existingPerson)
ctx.SaveChanges();

这篇关于实体框架4 - ADDOBJECT VS附加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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