Object structure changes

Does it make sense to save changes after a read-only action? Objects are loaded into the cache, but nothing changes, should you save the changes that need to be called before deleting?

+5
source share
2 answers

From doc ( DbContext.SaveChanges ) :

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

It makes no sense to call SaveChanges if you have not made any changes to your context.

You can read more about this here.

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

  • Added: object is tracked by context, but does not yet exist in the database
  • No change: the object is tracked by context and exists in the database, and its property values ​​do not change from the values ​​in the database
  • Changed: the object is tracked by context and exists in the database, and some or all of its property values ​​have been changed
  • Deleted: the object is tracked by context and exists in the database, but was marked for deletion from the database the next time SaveChanges is called
  • Disabled: object is not tracked by context

SaveChanges does different things for objects in different states:

  • Immutable objects are not affected by SaveChanges. Updates are not sent to the database for objects in the "No change" state.
  • Added objects are inserted into the database and then become unchanged when SaveChanges returns.
  • Modified objects are updated in the database and then become immutable when SaveChanges returns.
  • Deleted objects are deleted from the database and then deleted from the context.
+5
source

You do not need to call SaveChanges() if you are not doing any Add or Update in Entity.

0
source

Source: https://habr.com/ru/post/1243027/


All Articles