Chandra Mohan
Chandra Mohan

Reputation: 771

row version don't match after deleting and reinsert

I have a table with rowversion column .

I am getting the data with below query

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

After fetching the data I will update status using below query based on 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# Code For calling the selection store procedure

        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);
        }

C# code for calling updation stored procedure Update_Events

        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);
        }

But The updation only happening for the first time. If I delete the row and insert it again and then if I run above stored procedures(Select & Updation ) i am unable to update the status as rowversion is getting mismatched with DB Value rowVersion.

I am executing the Updation stored procedure using Entity Framework Object context ExecuteFunction.

I am not sure whether it is a problem with EF or Auto mapper which we use to map entity to Domain model.

I would appreciate if somebody help me to identity the issue?.

If you need any further information please let me know.

Upvotes: 0

Views: 376

Answers (0)

Related Questions