---
title: "How to handle concurrent updates in C# dotnet webapi without Entity Framework?"  
description: "How to handle concurrent updates in C# dotnet webapi without Entity Framework?"  
author: "Revati S Misra"  
published: 2023-09-05  
updated: 2023-09-06  
canonical: https://www.mindstick.com/forum/159839/how-to-handle-concurrent-updates-in-c-sharp-dotnet-webapi-without-entity-framework  
category: "c#"  
tags: ["c#", ".net", "entity framework"]  
reading_time: 2 minutes  

---

# How to handle concurrent updates in C# dotnet webapi without Entity Framework?

How to [handle](https://www.mindstick.com/articles/311004/suede-skillet-handle-cover) concurrent [updates](https://yourviews.mindstick.com/view/85235/global-efforts-to-combat-climate-change-updates-and-initiatives) in C# [dotnet](https://www.mindstick.com/interview/2187/what-is-the-gac) [webapi](https://www.mindstick.com/forum/12745/how-do-i-use-webapi-rest-correctly-when-other-params-are-needed) without [Entity Framework](https://www.mindstick.com/articles/1566/crud-operations-using-entity-framework-code-first-approach)?

## Replies

### Reply by Aryan Kumar

There are a several ways to handle concurrent updates in C# .NET Web API without [Entity](https://www.mindstick.com/forum/160562/database-connectivity-using-entity-framework-database-first-approach) [Framework](https://www.mindstick.com/forum/34615/software-framework-vs-library). Here are some of the most common methods:

**Use optimistic concurrency:** This method uses the version number to track changes to the record. When the user updates the record, the version number is incremented. When another user tries to update the same record, the server checks the version number. If the version number has changed, the second user's update will be rejected.

**Use pessimistic concurrency:** This method locks the record while it is being updated. This prevents other users from updating the record at the same time.

**Use a database transaction:** This method includes all of the updates to a record in a database transaction. If any of the update fails, the entire transaction will be rolled back.

**Use a distributed lock:** This method uses a third-party service to obtain profile key. This prevents other users from updating the record until the lock is released.

The best method to use will depend on your specific application requirements.

Here is an example of using optimistic concurrency in a C# .NET Web API without Entity Framework:

C#

```plaintext
public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Version { get; set; }
}

public class PersonController
{
    public async Task<IActionResult> UpdatePerson(int id, Person person)
    {
        // Get the person from the database.
        var existingPerson = await _context.People.FindAsync(id);

        // Check the version number.
        if (existingPerson.Version != person.Version)
        {
            // The record has been updated by another user.
            throw new BadRequestException("The record has been updated by another user.");
        }

        // Update the record in the database.
        _context.People.Update(person);
        await _context.SaveChangesAsync();

        // Return the updated person.
        return Ok(person);
    }
}
```

In this example, we first get a person from the database using the `FindAsync()` method. Then, we check the version number of the directory. If the version number has changed, we will raise an exception. Otherwise, we update the record in the database and return the person who updated.


---

Original Source: https://www.mindstick.com/forum/159839/how-to-handle-concurrent-updates-in-c-sharp-dotnet-webapi-without-entity-framework

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
