-1

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

 www.google.com
4

1 回答 1

1

您所说的确实如此SaveChanges()it check if the entity is in modified state然后它会保存更改,以便它们可以反映在数据库中。

在创建操作的情况下create a new object and add that to your entity。再一次,entity state is modified所以你也需要在这里调用 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.
于 2013-10-11T04:07:50.610 回答