Entity framework 实体框架CRUD操作中的SaveChanges函数

Entity framework 实体框架CRUD操作中的SaveChanges函数,entity-framework,Entity Framework,他只是想知道,在实体框架中,当我们调用savechages()函数时,它会比较实体中的当前值和原始值,并更新数据库。对吗?。我还有一个疑问,如果上述条件是正确的,那么是否需要在create operation?中调用savechanges()?。为什么我们称之为create操作 www.google.com 您所说的SaveChanges(),检查实体是否处于修改状态,然后保存更改,以便在数据库中反映出来 在创建操作中,您可以创建一个新对象并将其添加到实体中。再次修改实体状态,因此您也需要在

他只是想知道,在实体框架中,当我们调用savechages()函数时,它会比较实体中的当前值和原始值,并更新数据库。对吗?。我还有一个疑问,如果上述条件是正确的,那么是否需要在create operation?中调用savechanges()?。为什么我们称之为create操作

 www.google.com

您所说的
SaveChanges()
检查实体是否处于修改状态,然后保存更改,以便在数据库中反映出来

在创建操作中,您可以创建一个新对象并将其添加到实体中。再次修改
实体状态
,因此您也需要在此处调用savechanges()

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

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 does different things for entities in different states:

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.