Search code examples
c#entity-framework-4timestampsql-server-cerowversion

EF4 with SQL Compact 4, rowversion doesn't update on save


I have a simple console app using SQL Compact 4.0 and entity frameworks 4. The database has a single table called Section which has three columns: Id (StoreGeneratedPattern: Identity, Type: Int32), Title (Type: string) and TimeStamp (StoreGeneratedPattern: Computed, ConcurrencyMode: Fixed, Type: Binary, MaxLength: 8). The TimeStamp column is actually a rowversion type in SQL Compact.

I have the following code in main:

Section section = new Section();
section.Title = "Hello";

using (Entities1 context = new Entities1())
{
    context.Sections.AddObject(section);                
    context.SaveChanges();

    section.Title = "Changed";
    context.SaveChanges();
}

This code throws a concurrency exception because the TimeStamp column is not getting updated from the database after the first SaveChanges() method. Note that it works fine in SQLServer2008.

Is this a bug in Compact or am I missing something?

Thanks,

Darren


Solution

  • I found the problem. Turns out it is a bug in the edmx designer where the property for StoreGeneratedPattern is not being written to the underlying xml file. It is saved only in the CSDL part but not in the SSDL part See http://www.ladislavmrnka.com/2011/03/the-bug-in-storegeneratedpattern-fixed-in-vs-2010-sp1/ Note that in that blog he says it is fixed in VS2010 SP1, well I have that version and it is not fixed in mine!

    Entering it by hand into the xml file fixed the problem. Unfortunately subsequent changes clear it again.

    Darren