带有 SQL Compact 4 的 EF4,行版本在保存时不会更新

发布于 2024-11-19 12:33:54 字数 771 浏览 2 评论 0原文

我有一个使用 SQL Compact 4.0 和实体框架 4 的简单控制台应用程序。数据库有一个名为 Section 的表,其中包含三列: Id (StoreGeneratePattern: Identity ,类型:Int32)Title (类型:字符串)TimeStamp (StoreGeneratePattern:已计算,并发模式:固定,类型:二进制,最大长度:8)。 TimeStamp 列实际上是 SQL Compact 中的 rowversion 类型。

我在 main 中有以下代码:

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

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

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

此代码引发并发异常,因为在第一个 SaveChanges() 方法之后未从数据库更新 TimeStamp 列。请注意,它在 SQLServer2008 中运行良好。

这是 Compact 中的错误还是我遗漏了什么?

谢谢,

达伦

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深爱成瘾 2024-11-26 12:33:54

或许:
context.Refresh(System.Data.Objects.RefreshMode.ClientWins, 部分);
第一次 SaveChanges 后

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

相权↑美人 2024-11-26 12:33:54

我发现了问题。事实证明,这是 edmx 设计器中的一个错误,其中 StoreGeneratePattern 的属性未写入底层 xml 文件。它仅保存在 CSDL 部分中,而不保存在 SSDL 部分中 请参阅 http://www.ladislavmrnka.com/2011/03/the-bug-in-store generatedpattern-fixed-in-vs-2010-sp1/ 请注意,他在该博客中这么说在VS2010 SP1中已修复,我有那个版本,但在我的版本中尚未修复!

手动将其输入到 xml 文件中解决了该问题。不幸的是,随后的更改再次清除了它。

达伦

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

孤云独去闲 2024-11-26 12:33:54

听起来像是 EF 与 SQL CE 结合使用时不受支持的功能。也许这会提供更多见解:

EF / SQL CE / 自动生成主键

Rowversion 列无法在 SQL CE 中更新 - 也许这与 SaveChanges 时 EF 尝试更新它发生冲突

但是有一说一:

莱克西斯,
您所讨论的 4.1.1 已知问题仅当您尝试对具有 Identity 或 Rowversion 列的表执行 DML 操作时才会出现问题。就 Select 语句而言,没有问题,一切都应该有效。如果您需要在应用程序中针对 SQLCE 使用 DML 语句,则架构中不应包含 Identity 和 Rowerversion 列。其余一切都会正常工作,实体框架将为您添加数据。如果此后您仍然遇到任何问题,请告诉我,我很乐意为您提供帮助。问候拉维·坦登。

它位于帖子评论下方的某处。其中 DML 代表数据操作语言(读作:插入/更新/删除)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文