Solved

Database lock in Entity framework

Posted on 2014-03-31
3
1,928 Views
Last Modified: 2014-12-06
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
Comment
Question by:Vikash p
3 Comments
 
LVL 39

Assisted Solution

by:lcohan
lcohan earned 250 total points
ID: 39967419
" 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
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 250 total points
ID: 39967560
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
 
LVL 3

Expert Comment

by:bromose
ID: 40484531
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question