实体框架保存更改 [英] Entity framework save changes

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

问题描述

在只读操作之后是否有保存更改的意义?实体已加载到缓存中,但没有任何更改,是否应该在处理之前调用保存的更改?

Is there a point to save changes after a read only action? The entities are loaded to cache, but nothing changes, should save changes be called before dispose?

推荐答案

来自

将在此上下文中进行的所有更改保存到基础数据库中.

Saves all changes made in this context to the underlying database.

如果您未对上下文进行任何更改,则没有必要调用SaveChanges.

No there is no point in calling SaveChanges if you have not made any changes on your context.

您可以在此处

You can read more about this in detail here

实体可以处于EntityState枚举定义的五个状态之一.这些状态是:

An entity can be in one of five states as defined by the EntityState enumeration. These states are:

  • 已添加:上下文正在跟踪该实体,但该实体在数据库中尚不存在
  • 未更改:实体正在被上下文跟踪并且存在于数据库中,并且其属性值与数据库中的值没有变化
  • 已修改:实体正在被上下文跟踪并存在于数据库中,并且其某些或所有属性值已被修改
  • 已删除:该实体正在被上下文跟踪并且存在于数据库中,但是已被标记为在下次调用SaveChanges时从数据库中删除
  • 已分离:该实体未被上下文跟踪
  • Added: the entity is being tracked by the context but does not yet exist in the database
  • Unchanged: the entity is being tracked by the context and exists in the database, and its property values have not changed from the values in the database
  • Modified: the entity is being tracked by the context and exists in the database, and some or all of its property values have been modified
  • Deleted: the entity is being tracked by the context and exists in the database, but has been marked for deletion from the database the next time SaveChanges is called
  • Detached: the entity is not being tracked by the context

SaveChanges对处于不同状态的实体执行不同的操作:

SaveChanges does different things for entities in different states:

  • 未更改的实体不受SaveChanges的影响.处于未更改状态的实体的更新不会发送到数据库.
  • 将添加的实体插入数据库,然后在SaveChanges返回时保持不变.
  • 已修改的实体在数据库中进行更新,然后在SaveChanges返回时变为不变".
  • 已删除的实体将从数据库中删除,然后与上下文分离.
  • Unchanged entities are not touched by SaveChanges. Updates are not sent to the database for entities in the Unchanged state.
  • Added entities are inserted into the database and then become Unchanged when SaveChanges returns.
  • Modified entities are updated in the database and then become Unchanged when SaveChanges returns.
  • Deleted entities are deleted from the database and are then detached from the context.

这篇关于实体框架保存更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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