7

I am using Entity Framework with the Self-Tracking Entity T4 templates, which by default will generate a SQL Query setting all the properties on the Entity in an UPDATE statement. I only want an UPDATE statement that contains the properties modified.

I modified the T4 Template as specified in the book: Entity Framework Recipes: A Problem-Solution Approach page 503.

I changed to this line in the T4 Template:

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

Making the Entity track each property change instead of just tracking that the Entity changed.

And also

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

After making these changes I got the desired result of SQL statement with only the modified values/properties in the UPDATE statement. However, there was a strange side effect. When updating a nullable INT property from null to something other than null, the change of that was ignored by Entity Framework. The Self-Tracking Models show the change in the ChangeTracker with the accurate OriginalValue null, but when Entity Framework tried to generate the UPDATE SQL it did not see that property change if the original value null and the new value is not null. I worked if the original value was not null and value gets changed.

It seems to work ok on a string property going from null to a non-null value, but int? is not working.

Does anyone have any ideas?

John Egbert
  • 4,878
  • 5
  • 28
  • 44
  • If you post code or XML, **please** highlight those lines in the text editor and click on the "code" button (101 010) on the editor toolbar to nicely format and syntax highlight it! – marc_s Sep 10 '10 at 04:58
  • 1
    The STE template may have a bug. This discussion may help http://stackoverflow.com/questions/3598550/are-nullable-foreign-keys-allowed-in-entity-framework-4/3601768 – Craig Stuntz Sep 10 '10 at 12:23
  • I don't have a foreign key involved with the table I am using. It's just 1 Entity with no Foreign keys. Your link above appears to be the same problem, but still no solution. – John Egbert Sep 10 '10 at 15:23
  • Do you know how to do this in EF5? I don't find the above mentioned lines in the Context.tt file – Narayana Jan 07 '14 at 12:50

1 Answers1

1

If helpful, have found post that fixes this: fix update of nullable column.

Tim Cooper
  • 144,163
  • 35
  • 302
  • 261