Solved

Database lock in Entity framework

Posted on 2014-03-31
3
1,701 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 62

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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now