开发者

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);          开发者_StackOverflow中文版      
    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


Maybe: context.Refresh(System.Data.Objects.RefreshMode.ClientWins, section); after first SaveChanges


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


Sounds like an unsupported feature in EF in combination with SQL CE. Maybe this will give some more insight:

EF / SQL CE / Auto-generated primary keys

Rowversion column cannot be updated in SQL CE - maybe this conflicts with EF trying to update it when you SaveChanges

But this one says it all:

Lexis, The 4.1.1 known issue you are talking about will cause problems only when you try to do DML operations on a table with Identity or Rowversion column. In terms of Select Statements there is no issue, everything should work. If you need to have DML statements in your application against SQLCE, you should not have Identity and Rowerversion column in schema. Rest everything will work fine and Entity Framework will add the data for you. If you still face any issues after this, please let me know, I will be glad to help. Regards Ravi Tandon.

It's somewhere down the comments on the post. Where DML stands Data Manipulation Language (read: Insert/Update/Delete).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜