删除并重新插入后,行版本不匹配

时间:2016-01-05 13:42:41

标签: c# sql-server entity-framework automapper rowversion

我有一个带有rowversion列的表。

我通过以下查询获取数据

BEGIN TRANSACTION EVENTS 


Declare @eventId int
Create table #tempEvents
(
    [EventID] [int]  NOT NULL,
)
Insert into #tempEvents
(
[EventID] 
)
(Select top(@TopN) [EventID]   FROM [dbo].[PolicyEventsOminibus] WITH (UPDLOCK,READPAST) WHERE [PublishStatus] = @PublishStatus)

update [PolicyEventsOminibus] set [PublishStatus]=1 where Eventid in (Select #tempEvents.eventid from #tempEvents)

Select  [PolicyEventsOminibus].* FROM [dbo].[PolicyEventsOminibus] WITH (UPDLOCK,READPAST) where Eventid IN(Select #tempEvents.eventid from #tempEvents)

drop TABLE #tempEvents
commit TRANSACTION EVENTS

获取数据后,我将使用以下基于rowversion的查询更新状态

 Create   PROCEDURE [dbo].[Update_Events]
(
@Publishstatus int,
@PrevPublishstatus int,
@LastUpdatedTimeStamp varchar(100),
@EventID int,
@RowVersion rowversion  
) 
AS
Update PolicyEventsOminibus
SET
Publishstatus = @Publishstatus ,
LastUpdatedTimeStamp  = @LastUpdatedTimeStamp
where EventID = @EventID and [RowVersion] = @RowVersion and Publishstatus =@PrevPublishstatus

C#代码用于调用选择存储过程

        public List<PolicyEventsOminibus> GetEventsFromOminibus(int publishStatus, int topN)
        {
            var dmvPlcyEvnts = this.contextObj.GetPolicyEventsOminibusbyEventsContext(publishStatus, topN);
            return (List<PolicyEventsOminibus>)dmvPlcyEvnts.ToList();
        }

        public virtual ObjectResult<PolicyEventsOminibus> GetPolicyEventsOminibusbyEvents(Nullable<int> publishStatus, Nullable<int> topN)
        {
            var publishStatusParameter = publishStatus.HasValue ?
                new ObjectParameter("PublishStatus", publishStatus) :
                new ObjectParameter("PublishStatus", typeof(int));

            var topNParameter = topN.HasValue ?
                new ObjectParameter("TopN", topN) :
                new ObjectParameter("TopN", typeof(int));

            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<PolicyEventsOminibus>("GetPolicyEventsOminibusbyEvents", publishStatusParameter, topNParameter);
        }

用于调用更新存储过程Update_Events的C#代码

        public int UpdateEventPublishStatus(int eventId, int pubStatus, byte[] rowVersion, int? prevpubStatus)
        {
            int result = this.contextObj.UpdateDmvPolicyEventsOminibus(pubStatus, prevpubStatus, DateTime.Now.ToString("ddMMyyyyhhmmss"), eventId, rowVersion);
            return result;
        }

         public virtual int Update_DMVPolicyEventsOminibus(Nullable<int> publishstatus, Nullable<int> prevPublishstatus, string lastUpdatedTimeStamp, Nullable<int> eventID, byte[] rowVersion)
        {
            var publishstatusParameter = publishstatus.HasValue ?
                new ObjectParameter("Publishstatus", publishstatus) :
                new ObjectParameter("Publishstatus", typeof(int));

            var prevPublishstatusParameter = prevPublishstatus.HasValue ?
                new ObjectParameter("PrevPublishstatus", prevPublishstatus) :
                new ObjectParameter("PrevPublishstatus", typeof(int));

            var lastUpdatedTimeStampParameter = lastUpdatedTimeStamp != null ?
                new ObjectParameter("LastUpdatedTimeStamp", lastUpdatedTimeStamp) :
                new ObjectParameter("LastUpdatedTimeStamp", typeof(string));

            var eventIDParameter = eventID.HasValue ?
                new ObjectParameter("EventID", eventID) :
                new ObjectParameter("EventID", typeof(int));

            var rowVersionParameter = rowVersion != null ?
                new ObjectParameter("RowVersion", rowVersion) :
                new ObjectParameter("RowVersion", typeof(byte[]));

            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("Update_DMVPolicyEventsOminibus", publishstatusParameter, prevPublishstatusParameter, lastUpdatedTimeStampParameter, eventIDParameter, rowVersionParameter);
        }

但是更新只是第一次发生。如果我删除该行并再次插入它然后如果我运行上面的存储过程(选择和更新)我无法更新状态,因为rowversion与DB Value rowVersion不匹配。

我正在使用Entity Framework Object上下文ExecuteFunction执行Updation存储过程。

我不确定使用EF或Auto mapper来解决实体到域模型的问题。

如果有人帮我识别问题,我将不胜感激?

如果您需要任何进一步的信息,请告诉我。

0 个答案:

没有答案