How to implement row level locking in ASP.NET.Core Razor Page application?

Htet Lin 46 Reputation points
2021-02-18T07:57:47.21+00:00

I've an ASP.NET Core Razor Page application. Two users logged in simultaneously and in a Grid page both users choose same record.
User1 modifies some data and click submit button. User2 modifies data and click submit button.
At this time I would like to show a Pop-up message saying that Another User (ie User1) modified that data. Upon clicking Ok button I would like to rollback the User2's transaction. How to achieve this. Please guide me.

It would be great if I get any sample application or code. Thanks.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,312 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Wang-MSFT 1,061 Reputation points
    2021-02-19T06:55:12.347+00:00

    Hi, @Htet Lin ,

    You could try the approch below:

    1. Define your model with updated_by and updated_at properties.
    2. Before updating, Check whether the update time from the posted value is later than the updateAt in DB or not. If late, return failed.
    3. If failed, show a pop-up message to reload data again.

    Example:

    public class Product    {  
        public int Id { get; set; }  
        public string Name { get; set; }  
      
        public string UpdatedBy { get; set; }  
        public DateTime UpdatedAt { get; set; }  
    }  
    

    Eachtime the views display the Product will contain these two properties.

    Product
    Id = 1,
    Name = "abc",
    UpdatedBy= "admin"
    UpdatedAt ="12/19/2020 12:00:00"
    ---
    User2 updated the Product { Id = 1, Name = "xyz", UpdatedBy= "user2", UpdatedAt ="12/19/2020 12:00:00" } Success

    Product in DB
    Id = 1,
    Name = "xyz",
    UpdatedBy= "user2"
    UpdatedAt ="12/19/2020 12:10:00"
    ---

    If User1 submit Product { Id = 1, Name = "efg", UpdatedBy= "user1", UpdatedAt ="12/19/2020 12:00:00" }, it will be failed.

    If the answer doesn’t solve your issue, please provide more details of error that will help us track down what’s happening.
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,
    Michael Wang


0 additional answers

Sort by: Most helpful