I’m currently developing an app using Entity Framework 4.1 and MySQL. I want to use optimistic concurrency and therefore need to create a table structure which allows EF to detect concurrency issues. My goal is something similar to this: http://blogs.msdn.com/b/alexj/archive/2009/05/20/tip-19-how-to-use-optimistic-concurrency-in-the-entity-framework.aspx.
My problem is that the timestamp-type is different in MySQL compared to MS SQL Server. In addition to that neither timestamp nor datetime offer sub-second precision in MySQL (http://feedblog.org/2007/05/26/why-doesnt-mysql-support-millisecond-datetime-resolution/). These types would therefore be quite bad in detecting concurrency-problems.
What other data-type could I use to solve this? I was thinking of maybe using a Guid. There are two potential problems with this approach though: 1. MySQL stores Guids as char(36) making them very inefficient. 2. I’m not sure if the EF requires the row-version to be strictly increasing or if it’s enough that it’s unique.
Big caveat: NOT TESTED – just thinking aloud.
EF supports override of
SaveChanges, so perhaps one option is to define an interface such as:and add an
int RowVersionproperty/field to both your model class(es) and the database table(s), and usepartial classto implement this interface (using implicit interface implementation):Then override
SaveChanges, something like:that should then function (in theory – untested) as a manually implemented row-version counter. Leave change-validation enabled for
RowVersion, and that should serve.