[英]EF 4 - Exception when updating table with timestamp column
I have created the following table in SQL Server 我已经在SQL Server中创建了下表
CREATE TABLE [dbo].[Role](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](20) NOT NULL,
[CreatedDate] [datetime] NULL,
[TIMESTAMP] [timestamp] NOT NULL,
[ModifiedDate] [datetime] NULL,
CONSTRAINT [PK_TBL_ROLES] PRIMARY KEY CLUSTERED ([Id] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY].
With EF, I created a ste class with timestamp column of type byte[] which is readonly. 使用EF,我创建了一个带有类型为byte []的timestamp列的ste类,该类为只读。
I retrieve an object from my db using the datacontext eg 我使用datacontext从我的数据库中检索一个对象,例如
var roleObject = roleService.getObject(id);
now I change the rolename as follows 现在我将角色名称更改如下
roleObject.Name = "New Name";
roleObject.ModifiedDate = DateTime.Now;
finally I call my repository to persist the object using the following generic method 最后,我使用以下通用方法调用存储库以持久化对象
public void PersistUpdatedItem(T entity){
_ctx.ApplyCurrentValues(typeof (T).Name, entity);
_ctx.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
_ctx.SaveChanges();
}
Note ctx
is my session object and T is the entity class. 注意
ctx
是我的会话对象,T是实体类。 At this point I get an exception 在这一点上,我得到一个例外
Cannot update Timestamp Column
无法更新时间戳列
Can someone please assist me in solving this one. 有人可以帮我解决这个问题吗?
thanks 谢谢
Two points: 两点:
Timestamp
property "read only". Timestamp
属性设置为“只读”。 EF must be able to set that value so the property must have setter and accessibility of the setter must be same as accessibility defined in the designer . Timestamp
property must be configured with Concurrency Mode Fixed and StoreGenerated Pattern Coumputed . Timestamp
属性必须配置为“并发模式固定”和“ StoreGenerated模式已累计” 。 Both these configurations are defined in properties window in the designer. 只需从模型对象中删除时间戳即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.