简体   繁体   English

EF4 与 SQL Compact 4,rowversion 在保存时不更新

[英]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) .我有一个使用 SQL Compact 4.0 和实体框架 4 的简单控制台应用程序。数据库有一个名为Section的表,其中包含三列: Id (StoreGeneratedPattern: Identity, Type: Int32)Title (Type: string)TimeStamp (StoreGeneratedPattern:计算,并发模式:固定,类型:二进制,最大长度:8) The TimeStamp column is actually a rowversion type in SQL Compact. TimeStamp 列实际上是 SQL Compact 中的 rowversion 类型。

I have the following code in main:我在 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.此代码引发并发异常,因为在第一个 SaveChanges() 方法之后未从数据库更新 TimeStamp 列。 Note that it works fine in SQLServer2008.请注意,它在 SQLServer2008 中运行良好。

Is this a bug in Compact or am I missing something?这是 Compact 中的错误还是我遗漏了什么?

Thanks,谢谢,

Darren达伦

Maybe: context.Refresh(System.Data.Objects.RefreshMode.ClientWins, section);也许:context.Refresh(System.Data.Objects.RefreshMode.ClientWins, section); after first SaveChanges在第一次 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.原来这是 edmx 设计器中的一个错误,其中 StoreGeneratedPattern 的属性未写入底层 xml 文件。 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!它仅保存在 CSDL 部分中,而不保存在 SSDL 部分中 请参阅http://www.ladislavmrnka.com/2011/03/the-bug-in-storegeneratedpattern-fixed-in-vs-2010-sp1/注意在那篇博客他说它在 VS2010 SP1 中已修复,我有那个版本,但在我的中没有修复!

Entering it by hand into the xml file fixed the problem.手动将其输入到 xml 文件中解决了该问题。 Unfortunately subsequent changes clear it again.不幸的是,随后的更改再次清除了它。

Darren达伦

Sounds like an unsupported feature in EF in combination with SQL CE.听起来像是 EF 中不受支持的功能与 SQL CE 结合使用。 Maybe this will give some more insight:也许这会提供更多的见解:

EF / SQL CE / Auto-generated primary keys EF / SQL CE / 自动生成的主键

Rowversion column cannot be updated in SQL CE - maybe this conflicts with EF trying to update it when you SaveChanges无法在 SQL CE 中更新 Rowversion 列 - 这可能与 EF 在 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. Lexis,您所说的 4.1.1 已知问题只会在您尝试对具有 Identity 或 Rowversion 列的表执行 DML 操作时引起问题。 In terms of Select Statements there is no issue, everything should work.就 Select 语句而言,没有问题,一切正常。 If you need to have DML statements in your application against SQLCE, you should not have Identity and Rowerversion column in schema.如果您需要在应用程序中针对 SQLCE 使用 DML 语句,则架构中不应有 Identity 和 Rowerversion 列。 Rest everything will work fine and Entity Framework will add the data for you. Rest 一切正常,Entity Framework 将为您添加数据。 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). DML 代表数据操作语言(阅读:插入/更新/删除)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM