question

HtetLin avatar image
0 Votes"
HtetLin asked HtetLin commented

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

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.

dotnet-aspnet-core-general
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

miwan2-msft avatar image
0 Votes"
miwan2-msft answered HtetLin commented

Hi, @HtetLin ,

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


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for your Answer .

0 Votes 0 ·