实体框架4 - AddObject与Attach [英] Entity Framework 4 - AddObject vs Attach

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

问题描述

我最近一直在使用Entity Framework 4,并且稍微混淆了何时使用 ObjectSet.Attach 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.

从我的理解:


  • 当实体已经使用附加存在于系统中

  • 创建全新Entity时使用AddObject

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

如果我修改现有的人,我可以这样做: / p>

If i'm modifying an existing Person, i do this:

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

请记住,这是一个非常简单的例子,实际上正在使用Pure POCO(无代码生成),Repository模式(不处理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.

现在,我的问题 - 我还没有找到一个场景,我不得不使用 strong>。

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

我在这里缺少什么?什么时候需要使用Attach?

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

编辑

只是为了澄清,我正在寻找何时使用Attach over 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.

一个用于检索Person(.SingleOrDefault),另一个用于执行UPDATE(.SaveChanges)。

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

如果(对于某些原因),我已经知道系统中存在Joe Bloggs,为什么要先查询一下额外的查询?我可以这样做:

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.

推荐答案

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.

ObjectContext.Attach ObjectSet.Attach


另一方面,附件用于实体在数据库中已经存在 。而不是将
EntityState设置为添加,将结果附加到不变的 EntityState中,这意味着它没有更改,因为它附加到上下文。您附加的对象假定存在于数据库中。如果在附加对象之后修改对象,调用SaveChanges时,将使用EntityKey的值来更新(或删除)适当的行,方法是在数据库表中找到匹配的ID。



此外,使用Attach方法,您可以定义ObjectContext中已存在但具有 的实体之间的关系>已自动连接基本上,Attach的主要目的是连接已经附加到ObjectContext的实体,并且不是新的,所以您不能使用Attach附加实体的EntityState。在这种情况下,您必须使用 Add()



例如,假设您的Person实体具有名为 Addresses 的导航属性,该属性是地址实体的集合。假设您已经从上下文中读取了两个对象,但是它们并不相关,并且您希望这样做:

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与Attach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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