bclubb wrote:
To do what you are wanting you should use option 1 instead. The purpose of option 2 is to allow you to update an entity without having to first fetch it.
You want to only execute your update if there is an actual change. To do this you need to fetch the entity first. Once you have done that you can set it's properties. If none of those properties are assigned new values then the entity will not be dirty. If you do change the value of any properties then those values will be updated in the database.
Thanks for the reply. At first I thought this was promising for not having to wrap each assignment in conditional code. Unfortunately "empty" fields set an entity to IsDirty. I've tried assigning both an empty string and a null, but in both cases, the entity is still updated to IsDirty. If both fields have values, and the values are equal, it doesn't mark the field as dirty even though I've assigned it. A standard scenario might be a middle name, which is left NULL or EMPTY in most cases. Without wrapping each assignment with a conditional, the entity will be marked dirty, even if this field hasn't changed.
It would be nice if there was some kind of empty field assignment that indicated the field hasn't really changed. Null would be a natural now that V2 supports nullable fields, but I know there's at least one other thread that covers the thinking behind why this is a bad thing. So for now I guess I get used to:
if ( oldValue1.Trim() != newValue1.Trim() )
entity.Field1 = newValue1.Trim();
if ( oldValue2.Trim() != newValue2.Trim() )
entity.Field2 = newValue2.Trim();
...
if ( oldValue34.Trim() != newValue34.Trim() )
entity.Field34 = newValue34.Trim();