带有 SQL Compact 4 的 EF4,行版本在保存时不会更新
我有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
或许:
context.Refresh(System.Data.Objects.RefreshMode.ClientWins, 部分);
第一次 SaveChanges 后
Maybe:
context.Refresh(System.Data.Objects.RefreshMode.ClientWins, section);
after first SaveChanges
我发现了问题。事实证明,这是 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
听起来像是 EF 与 SQL CE 结合使用时不受支持的功能。也许这会提供更多见解:
EF / SQL CE / 自动生成主键
Rowversion 列无法在 SQL CE 中更新 - 也许这与 SaveChanges 时 EF 尝试更新它发生冲突
但是此有一说一:
它位于帖子评论下方的某处。其中 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:
It's somewhere down the comments on the post. Where DML stands Data Manipulation Language (read: Insert/Update/Delete).