Click here to Skip to main content
15,868,340 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Folks,

in my ODATA service I use EF6 to work with SQL Server.
An EDMX is created also for the table Departments. In that table there is a TimeStamp field for Concurrency purposes. And yes I have added [Timestamp] data annotation in the code. And in the EDMX the field is FIXED for Concurrency Mode.

When I select a department in my website datagrid and update the field I get the message:
Cannot update a timestamp column.


In SQlProfiler I see the update passing:
SQL
exec sp_executesql N'UPDATE [dbo].[Department]
SET [ModificationDate] = @0, [ModifiedBy] = @1
WHERE (([Id] = @2) AND ([ModificationDate] = @3))
',N'@0 binary(8),@1 int,@2 int,@3 binary(8)',@0=0x0000000000000805,@1=27,@2=3,@3=0x0000000000000805
go


And then I notice the curious thing. It wants to update the ModificationDate field (Timestamp) while I want to update the field DepartmentName in the entity.

How can I overcome this strange behaviour?

UPDATE!
I have set the Column to Computed and now the column is not added to the SET part of the query.
But why the field DepartmentName is not being set in the update query by EF?

What I have tried:

This is my code. The Departmentname is set! (SelectedEntity is set in the aspx.cs code when the RowUpdating command is being executed.
C#
if (SelectedEntity == null)
                   SelectedEntity = new Department();
               else
                   SelectedEntity = MarinGlobalConnection.MarinGlobalDataService._marinGlobal.Departments.First(x => x.Id == SelectedEntity.Id);
               SelectedEntity.DepartmentName = afdelingNaam;
               SelectedEntity.IsDeleted = isdeleted;
               SelectedEntity.ModifiedBy = CurrentUser.Instance.CurrentConnected == null ? (int?)null : CurrentUser.Instance.CurrentConnected.ID;
               if (SelectedEntity.Id == 0)
                   MarinGlobalConnection.MarinGlobalDataService._marinGlobal.AddToDepartments(SelectedEntity);
               else
               {
                   if (CompareModificationdate(modificationdate))
                       MarinGlobalConnection.MarinGlobalDataService._marinGlobal.UpdateObject(SelectedEntity);
               }
               await SaveChangesAsync();
Posted
Updated 17-Aug-18 4:23am
v2

1 solution

You never gave any details about your database schema as coded in EF, so this is just a guess.

What you're seeing is the expected behavior.

You are NOT supposed to make any changes to a column tagged [Timestamp] in EF. Timestamp fields are entirely managed by EF. They are used to manage concurrency and should never be a part of your data model.
 
Share this answer
 
Comments
Herman<T>.Instance 20-Aug-18 8:35am    
The pint is that despite the use of a dataannotaion in EF the field was updated by query generated by EF6. I have set the column to Computed. No EF6 doesn't toch my column anymore.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900