为什么要使用附件来更新Entity Framework 6? [英] Why use Attach for update Entity Framework 6?

查看:69
本文介绍了为什么要使用附件来更新Entity Framework 6?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在通过EF搜索执行CRUD操作的最佳方法时,我注意到强烈建议使用 Attach() Find() 方法,然后再更新实体。它运作良好,并且根据EF文档,这些方法将实体获取到对我来说很清楚的上下文。但是followind代码使我非常困惑

  public void Update(对象实体)
{
记录= new Record(){
id = 1,
value = 5
};
使用(SomeContext ctx = new SomeContext())
{
ctx.Entry(record).State = EntityState.Modified;
ctx.SaveChanges();
}
}

假设我们在数据库中有一条id为1的记录。在这种情况下,上面的代码将更新记录(将值设置为5)。问题是为什么它起作用?然后为什么要使用 Attach()?据我了解,该记录没有以任何方式附加到上下文。我阅读了这本书本教程,但它们使用2查询方法。我也冲浪,但是没有找到问题的答案。请给我提供解释或一些好的数学知识。

解决方案

如果您拥有数据库中已经存在但当前未被上下文跟踪的实体,则该实体在您的情况下是正确的-那么您可以使用 DbSet 上的 Attach 方法告诉上下文跟踪实体。
因此,总而言之,附加方法的作用是在上下文中跟踪实体并将其状态更改为 未更改 。在此之后修改属性时,跟踪更改将为您将其状态更改为 已修改
如果您在上面显示了这种情况,则可以明确地告诉您状态是 Modified ,而且还可以将实体附加到上下文中。您可以在此帖子中找到详细的说明。。 p>

何时应使用附加方法?



当您拥有一个数据库中已经存在但想要进行一些更改的实体时:

  var实体=新实体{id = 1}; 
context.YourDbSet.Attach(entity);

//做一些改变...
entity.value = 5;

context.SaveChanges();

这是相同的:

  context.Entry(entity).State = EntityState。未更改; 

//做一些改变...
entity.value = 5;

context.SaveChanges();

何时应将实体的状态显式更改为已修改?



当您拥有一个数据库中已经存在但现在已经进行了更改的实体时。与您的示例相同的场景


While searching for the best practivies of performing CRUD operation via EF I noticed that it is highly recommended to use Attach() or Find() methods before updating an entity. It works well and according to EF documentation these methods fetch the entity to context that is quite clear for me. But the followind code confused me pretty much

public void Update(object entity)
{
    Record record = new Record() {
        id = 1,
        value = 5
    };
    using (SomeContext ctx = new SomeContext())
    {
        ctx.Entry(record).State = EntityState.Modified;
        ctx.SaveChanges();
    }
}

Assume we have a record with id = 1 in database. On this condition the code above will update the record (set the value to 5). The question is why it works? And then why should I use Attach()?. As far as I understand the record wasn't attached to context in any way. I read relevant chapters of this book and the tutorial but they use 2-query-approach. Also I surfed SO but didn't find answer on my question. Help me with explanation or some good matherials, please.

解决方案

If you have an entity that you know already exists in the database but which is not currently being tracked by the context - which is true in your case - then you can tell the context to track the entity using the Attach method on DbSet. So in summary what Attach method does is track the entity in the context and change its state to Unchanged. When you modify a property after that, the tracking changes will change its state to Modified for you. In the case you expose above you are telling explicitly that state is Modified but also to attach the entity to your context. You can find a detailed explanation in this post.

When should you use Attach method?

When you have an entity that you know already exists in the database but want to make some changes:

var entity= new Entity{id=1};
context.YourDbSet.Attach(entity); 

// Do some change...  
entity.value=5;

context.SaveChanges(); 

This is the same:

 context.Entry(entity).State = EntityState.Unchanged; 

// Do some change... 
entity.value=5; 

context.SaveChanges(); 

When should you change entity's State to Modified explicitly?

When you have an entity that you know already exists in the database but the changes have already been made then. The same scenario of your example

这篇关于为什么要使用附件来更新Entity Framework 6?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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