I have a table with a primary key of type char(7) (CharPK), a timestamp field (ts) and a date field (LastUpdated) which is automatically updated to getdate() by a table trigger. The timestamp field is defined with concurrency strategy "Client" in the EF model.
I define AfterGet interceptors for all string-fields (including the primary key field) in the table to trim the trailing spaces of char-fields and to guarantee that optional string fields do not return Nothing:
<AfterGet(EntityPropertyNames.(CharPK), AfterGet(EntityPropertyNames.OtherCharFields)>
Public Function TrimMultipleProps(ByVal strPropValue As String) As String
If Not String.IsNullOrEmpty(strPropValue) Then
If strPropValue.EndsWith(" ") Then
Return strPropValue.TrimEnd()
Else
Return strPropValue
End If
Else
'Guarantee to not return Nothing
Return String.Empty
End If
End Function
When I do this I cannot save new entities with PKs shorter than seven characters because the PK is trimmed and when DevForce reads the record after saving it detects a difference and throws an exception.
Therefore I suppress reading after save:
Dim so As New SaveOptions()
so.EntityTypesExcludedFromPostSaveRefetch = New List(Of Type) From {GetType(EntityWithCharPK)}
_mgr.DefaultSaveOptions = so
In this situation mgr.SaveChanges submits SQL like this:
exec sp_executesql N'update [dbo].[TableWithCharPK]
set [SomeColumn] = @0
where (([CharPK] = @1) and ([ts] = @2))
select [LastUpdated], [Created], [ts], [ComputedColumn1], [ComputedColumn2]
from [dbo].[TableWithCharPK]
where @@ROWCOUNT > 0 and [CharPK] = @1',N'@0 varchar(25),@1 char(7),@2 binary(8)',@0='f',@1='1234 ',@2=0x000000000011D6EA
At first I was impressed because exactly the necessary columns are read after the update.
The problem is, that none of the values in the select list are actually read into the entity.
The timestamp value in the entity stays unchanged after a save operation.
When I try to save this entity a second time, DevForce throws an error because it detects a concurrency problem because of the outdated timestamp value.
Is this a bug or am I'm missing something?
Thanks
Herbert