[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3044
  • Last Modified:

Database lock in Entity framework

Hi,

I want to lock table and only one user should able to view /update particular record.
I am using MVC 4.0, entity framework 6.0. Can any one tell me how to do it.

What if the browser hanged and user restart/refresh the browser ?
0
Vikash p
Asked:
Vikash p
2 Solutions
 
lcohanDatabase AnalystCommented:
" to lock table and only one user should able to view /update particular record."
You should never do that using SQL locks but some application locking mechanism and this should serve as a partial answer to your second question. If your "usera" locked a record and gest disconnected before "Save" you store that "UserID" [NOT the session ID which will be obviously gone] on the locked row in the SQL Table so next time the same user comes in will the app code will be able to retrieve for edit all previously locked row(s).
At the end you remove(make it NULL) the App UserId from that record so anyone else can work with it.
0
 
Fernando SotoCommented:
Hi bhanjpur;

Entity Framework works in a disconnected model, It connects to the database gets the records and then disconnects. It does this for all types of transactions. Entity Framework uses optimistic concurrency. When a user updates a record in the database it first checks to make sure that the record has not been modified since it first got the record. If the record has been modified since you got the record it throws an exception. This web page, Optimistic Concurrency Patterns, shows some ways to handle the exceptions.
0
 
bromoseCommented:
Hi.
Think you should use "Optimistic concurrency" (the recomended way by Microsoft). You will find a guide on "how to do" here: Handling Concurrency with the Entity Framework 6 in an ASP.NET
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now