DbSet.Attach(entity) vs DbContext.Entry(entity).State = EntityState.Modified [英] DbSet.Attach(entity) vs DbContext.Entry(entity).State = EntityState.Modified

查看:25
本文介绍了DbSet.Attach(entity) vs DbContext.Entry(entity).State = EntityState.Modified的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我处于分离的场景中并从客户端获取 dto 并将其映射到实体以保存它时,我会这样做:

When I am in a detached scenario and get a dto from the client which I map into an entity to save it I do this:

context.Entry(entity).State = EntityState.Modified;
context.SaveChanges();

对于什么是DbSet.Attach(entity)

或者当 EntityState.Modified 已经附加实体时,我为什么要使用 .Attach 方法?

or why should I use the .Attach method when EntityState.Modified already attaches the entity?

推荐答案

当您执行 context.Entry(entity).State = EntityState.Modified; 时,您不仅将实体附加到DbContext,您还将整个实体标记为脏.这意味着当您执行 context.SaveChanges() 时,EF 将生成一个更新语句,该语句将更新所有实体的字段.

When you do context.Entry(entity).State = EntityState.Modified;, you are not only attaching the entity to the DbContext, you are also marking the whole entity as dirty. This means that when you do context.SaveChanges(), EF will generate an update statement that will update all the fields of the entity.

这并不总是需要的.

另一方面,DbSet.Attach(entity) 将实体附加到上下文 将其标记为脏.相当于做了context.Entry(entity).State = EntityState.Unchanged;

On the other hand, DbSet.Attach(entity) attaches the entity to the context without marking it dirty. It is equivalent to doing context.Entry(entity).State = EntityState.Unchanged;

以这种方式附加时,除非您随后继续更新实体的属性,否则下次您调用 context.SaveChanges() 时,EF 将不会为此实体生成数据库更新.

When attaching this way, unless you then proceed to update a property on the entity, the next time you call context.SaveChanges(), EF will not generate a database update for this entity.

即使您打算更新实体,如果实体有很多属性(db 列)但您只想更新其中的几个,您可能会发现执行 DbSet 是有利的.附加(实体),然后只更新少数需要更新的属性.这样做会从 EF 生成更有效的更新语句.EF 只会更新你修改过的属性(与 context.Entry(entity).State = EntityState.Modified; 相反,这将导致所有属性/列被更新)

Even if you are planning on making an update to an entity, if the entity has a lot of properties (db columns) but you only want to update a few, you may find it advantageous to do a DbSet.Attach(entity), and then only update the few properties that need updating. Doing it this way will generate a more efficient update statement from EF. EF will only update the properties you modified (in contrast to context.Entry(entity).State = EntityState.Modified; which will cause all properties/columns to be updated)

相关文档:添加/附加和实体状态.

代码示例

假设您有以下实体:

public class Person
{
    public int Id { get; set; } // primary key
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

如果您的代码如下所示:

If your code looks like this:

context.Entry(personEntity).State = EntityState.Modified;
context.SaveChanges();

生成的 SQL 将如下所示:

The SQL generated will look something like this:

UPDATE person
SET FirstName = 'whatever first name is',
    LastName = 'whatever last name is'
WHERE Id = 123; -- whatever Id is.

请注意上面的更新语句将如何更新所有列,无论您是否真的更改了值.

Notice how the above update statement will update all the columns, regardless or whether you've actually changed the values or not.

相反,如果您的代码使用正常"附加如下:

In contrast, if your code uses the "normal" Attach like this:

context.People.Attach(personEntity); // State = Unchanged
personEntity.FirstName = "John"; // State = Modified, and only the FirstName property is dirty.
context.SaveChanges();

那么生成的update语句就不同了:

Then the generated update statement is different:

UPDATE person
SET FirstName = 'John'
WHERE Id = 123; -- whatever Id is.

如您所见,更新语句更新您将实体附加到上下文后实际更改的值.根据表的结构,这可能会对性能产生积极影响.

As you can see, the update statement only updates the values that were actually changed after you attached the entity to the context. Depending on the structure of your table, this can have a positive performance impact.

现在,哪种选择更适合您完全取决于您要尝试做什么.

Now, which option is better for you depends entirely on what you are trying to do.

这篇关于DbSet.Attach(entity) vs DbContext.Entry(entity).State = EntityState.Modified的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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