如何让实体框架停止设置 rowversion 字段?
我使用实体框架,并且表上有一个 rowversion (时间戳)字段用于并发。 但是,在更新实体对象时,它不断尝试将 rowversion 列设置为 null,并且出现错误:
“LmpDemoRequest”上的“VerCol”属性无法设置为“null”值。 您必须将此属性设置为“Byte[]”类型的非空值。
我在实体定义中有 VerCol 列,但无法删除“Setter”函数。
如何让实体框架停止尝试设置此列?
Im using the Entity Framework and I have a rowversion (timestamp) field on a table to use for concurrency. However, when updating the entity object, it keeps trying to set the rowversion column to null, and I get an error:
The 'VerCol' property on 'LmpDemoRequest' could not be set to a 'null' value. You must set this property to a non-null value of type 'Byte[]'.
I have the VerCol column within the entity definition, but I am unable to remove the "Setter" function.
How do I get the entity framework to stop attempting to set this column?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以为 RowVersion 字段传递任意有效值(例如 DateTime.Now)。 它们将被服务器生成的值覆盖。
对于 EF 的未来版本,应该支持“影子属性”,它存在于模型中,但不存在于您的类中。 该功能在这种情况下会很有用。
You can pass any arbitrary, valid values for the RowVersion fields (DateTime.Now for example). They will be overwritten with the server-generated values.
For future releases of EF, there should be support for "shadow properties", which exist in the model but not in your classes. That feature would be useful in situations such as this.
我遇到过这样的情况:视图包含来自视图中左侧连接的表的 RowVersion 列...因此该列有时可能为空。
但 EF4“知道”RowVersion 列不能为 null,因此即使在简单的 LINQ 查询中,它也会抛出 InvalidOperationException:
我最终不得不更改视图以将其用于 RowVersion 列,以便 EF 满意:
I had a case where a view included a RowVersion column from a table that was left joined in the view... so that column could be null sometimes.
But EF4 'knows' that a RowVersion column cannot be null, so even in a simple LINQ query, it was throwing an InvalidOperationException:
I finally had to change the view to use this for the RowVersion column, so that EF would be happy: