How to force Entity Framework to update only those properties that have been changed in SQL?

I use the Entity Framework with Self-Tracking Entity T4 templates that will generate SQL Query by default by setting all the properties in the Entity in the UPDATE statement. I only need an UPDATE statement containing the changed properties.

I modified the T4 template as described in the book: Entity Platform Recipes: A Problem Solving Approach , page 503.

I changed this line in the T4 template:

OriginalValueMembers originalValueMembers = new OriginalValueMembers(false, metadataWorkspace, ef); 

Create an Entity binding for each property change instead of simply tracking entity changes.

And

 context.ObjectStateManager.ChangeObjectState(entity, EntityState.Unchanged); 

After making these changes, I got the desired result of the SQL statement with only the changed values ​​/ properties in the UPDATE statement. However, there was a strange side effect. When updating the nullable INT property from null to a non-zero value, changing this parameter is ignored by the Entity Framework. Self-Tracking models show a change in ChangeTracker with the exact value InitialValue null, but when the Entity Framework tried to generate UPDATE SQL, he did not see that this property changed if the original value is null and the new value is not zero. I worked if the original value was not zero and the value changed.

The string property seems to work fine, going from zero to a non-zero value, but int? does not work.

Does anyone have any ideas?

+7
linq-to-entities entity entity-framework entity-framework-4 self-tracking-entities
source share
1 answer

If this is useful, you have found a message that fixes this: fix updating a column with a null value .

+1
source share

All Articles