Breeze optimistic concurrency exception saving datetime

136 views Asked by At

Here a tricky issue I have with breeze, entity framework and a datetime field.

I get an optimistic concurrency exception when saving the entity due to the date field having the current microseconds added to it on entering and on leaving the breeze system.

Hence entity framework thinks something else has edited the db since its original value is no longer equal to what is in the db (as the microseconds value is made up on the spot). SQL like the following is produced:

exec sp_executesql N'UPDATE [dbo].[Profiles] SET [DOB] = @0, [TotalTime] = @1, [Modified] = @2 WHERE (([Id] = @3) AND ([DOB] = @4)) ',N'@0 datetime2(7),@1 float,@2 datetime2(7),@3 uniqueidentifier,@4 datetime2(7)',@0='2015-06-22 15:21:43.4180000',@1=3210,@2='2015-06-22 15:22:52.7520000',@3='F944965E-9E18-E511-BF26-4CEB4272XXXX',@4='2015-06-22 15:21:01.5320000'

EF is looking for the date 2015-06-22 15:21:01.5320000 however the stored date is in fact 2015-06-22 15:21:01.533. A difference of 1 millisecond.

I'm not sure why this is?

The fix Im presently trying to implement is to add this function into the main.js of my app.

breeze.DataType.parseDateFromServer = function (source) { var dt = moment(source).toDate(); dt.setMilliseconds(0); return dt; };

This seems to have gotten me out of this bind.

1

There are 1 answers

0
JoeWarwick On

Doh! As it turned out someone had set the ConcurrencyMode of the DOB field in The entity diagram to 'Fixed' and this caused it to be heavily validated in this way. Changing that back to 'None' fixed it.