简体   繁体   English

EF 4-使用时间戳列更新表时发生异常

[英]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: 两点:

  • You cannot make 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 . EF必须能够设置该值,以便该属性必须具有setter,并且setter的可访问性必须与设计器中定义的可访问性相同。
  • Your 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.

相关问题 在Excel中更新列时出现Oledb异常 - Getting Oledb exception when updating column in excel 当 IDENTITY_INSERT 设置为 OFF 时,无法为表“TABLE”中的标识列插入显式值更新我的表 - Cannot insert explicit value for identity column in table 'TABLE' when IDENTITY_INSERT is set to OFF Exception Updating my table 列的“日期”数据类型在EF中未更新 - 'Date' datatype of a column is not updating in EF LINQ时间戳 - 自动更新时间戳列? - LINQ Timestamp - Automatically updating a timestamp column? EF迁移和时间戳列无法更新/运行 - EF migrations and timestamp column, cannot update/run EF6 表拆分和异常“ReferentialConstraint 中的依赖属性映射到存储生成的列。 列:'Id'。” - EF6 Table Splitting and exception “A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'Id'.” EF 6更新实体时行为异常 - EF 6 misbehaving when updating entity 使用 EF 更新 PK 时出错 - Error when updating PK with EF EF Core 6 - 从 SQL 表中删除列而不更新 C# Model - EF Core 6 - Remove column from SQL table without updating C# Model Npgsql EF 6:使用 ExecuteSqlInterpolatedAsync 时时间戳失败 - Npgsql EF 6: Timestamp fails when using ExecuteSqlInterpolatedAsync
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM