0

I'm using Entity Framework on MS-SQL database. I'm implementing a PUT web endpoint to update a "response set".

If the caller indicates forceSave = true, I want to clobber the record even though the RowVersion does not match.

This is my code, but it does not work....

    [HttpPut, Route("ResponseSets/{id:int}")]
    public IHttpActionResult UpdateResponseSet(int id, 
               [FromBody] ResponseSet responseSetForUpdate, 
               [FromUri]bool? forceSave = false)
    {
        var db = new MyContext();
        var responseSet = db.ResponseSets.FirstOrDefault(x => x.ResponseSetId == id);
        if (!responseSet.RowVersion.SequenceEqual(responseSetForUpdate.RowVersion) && !forceSave.Value)
        {
            return new NegotiatedContentResult<LockingUser>(HttpStatusCode.Conflict,
                new LockingUser(responseSet.LastUpdatedByUser), this);
        }
        responseSet.ResponseData = responseSetForUpdate.ResponseData;
        responseSet.LastUpdatedByUser = HttpContext.Current.User.Identity.Name ?? "Anonymous";
        responseSet.LastUpdatedDateTime = DateTime.Now;
        db.SaveChanges();
        return Ok();
    }

Mostly it works! However, sometimes I get DBConcurrencyException from the SaveChanges(). I think it is because there is a race condition. After I read the database with FirstOrDefault but before I write the database with SaveChanges another process might update the row and change the RowVersion! Is there any way to disable RowVersion checking?

I thought about using a Transaction begin/end, but obviously that won't help.

I thought about writing a loop, so that if I get DBConcurrencyException, then it will read the RowVersion again, and try to write again... until success... but jeez that feels hacky.

John Henckel
  • 10,274
  • 3
  • 79
  • 79
  • *"I thought about using a Transaction begin/end, but obviously that won't help"*, Why do you think that? This seems like the perfect situation for a transaction. (p.s. you really should be disposing your `db` object) – Scott Chamberlain Nov 03 '15 at 22:53
  • @ScottChamberlain well, now that you mention it, probably it will help... I'll try it tomorrow and report back. thanks. – John Henckel Nov 04 '15 at 05:36

0 Answers0