Modifying data via the DbContext The approach that you adopt to modifying entities depends on whether the context is currently tracking the entity being modified or not. In the following example, the entity is obtained by the context, so the context begins tracking it immediately. When you alter property values on a tracked entity, the context changes the EntityState for the entity to Modified and the ChangeTracker records the old property values and the new property values.
SaveChanges ; Since the ChangeTracker tracks which properties have been modified, the context will issue a SQL statement that updates only those properties that were changed: NET application, changes to an existing entity's property values can take place in a controller or service method, well away from the context.
In these cases, the context needs to be informed that the entity is in a modified state. This can be achieved in several ways: Attach method and then "walking the object graph" to set the state of individual properties within the graph explicitly.
State property, which is made available by the DbContext. Any related objects will not be tracked. Since the ChangeTracker is unaware of which properties were modified, the context will issue an SQL statement updating all property values apart from the primary key value.
Update author ; context. Once again, the context doesn't have any way of identifying which property values have been changed, and will generate SQL to update all properties. Where this method differs from explicitly setting the State property, is in the fact that the context will begin tracking any related entities such as a collection of books in this example in the Modified state, resulting in UPDATE statements being generated for each of them. Attach When you use the Attach method on an entity, it's state will be set to Unchanged, which will result in no database commands being generated at all.
All other reachable entities with key values defined will also be set to Unchanged. Those without key values will be marked as Added.
However, now that the entity is being tracked by the context, you can inform the context which properties were modified so that the correct SQL to update just those values is generated: Attach author ; context. This is useful in scenarios where you are dealing with complex object graphs that consist of various related entities in differing states. The following example replicates a scenario where an object graph is constructed outside of the context.
Then the TrackGraph method is used to "walk the graph": SaveChanges ; In this scenario, it is assumed that the author entity has not been changed, but the books might have been edited.
The TrackGraph method takes the root entity as an argument, and a lambda specifying the action to perform. In this case, the root entity, the author has its EntityState set to UnChanged. Setting the EntityState is required for the context to begin tracking the entity. Only then can related entities be discovered. Books have their EntityState set to Modified, which as in the previous examples, will result in SQL that updates every property on the entity: In the next example, the object graph is once again constructed outside of the context, but only the Isbn property of the books is modified.
Therefore other properties apart from the entity key are omitted: This results in SQL being generated that only updates the Isbn property value: