Tag: Release hang Pos method common cat char Ges code
See this article before the recommendation of this article in the garden has been introduced and written very good ~ ~ You can first look at him and see my
Concurrent
1. Pessimistic concurrency
Simply put, when a user accesses a piece of data, the data becomes read-only and the data becomes exclusive only if the user releases the data other users can modify this period if the user goes to a toilet for a lap without exiting then everyone else will have to wait for a long time obviously this is not the effect we expect or this article The focus of the discussion
2. Optimistic concurrency
Optimistic concurrency is relatively pessimistic and the data is not locked when the user reads the data. When a user updates the data, the system checks to see if the user has changed the data after reading the data. If other users update the data, an error is generated. This is also the main story of this article.
How to make EF optimistic concurrency is actually very simple only with three simple parts can create optimistic concurrency ~
Taking faculties as an example
The first step. Add a timestamp to the entity class
1537709052
Public byte[] Timestamp {get; set;}
The second step, in our Department edit view, add
@Html. hiddenfor (model = model. Timestamp)
Which means that the timestamp is read at the time of the reading.
The third step is to catch the concurrency exception to give the user a friendly hint
[HttpPost]
Public ActionResult Edit (Department Department)
{
Try
{
if (modelstate.isvalid)
{
Db. Entry (department). state = entitystate.modified;
Db. SaveChanges ();
Return redirecttoaction ("Index");
}
}
catch (Dbupdateconcurrencyexception ex)
{
Modelstate.addmodelerror (String. Empty, "friendly Tips");
}
}
Okay, then, if you have the following situation, a user opens the editing department another opens an editorial department. When one commits another, it prompts a friendly prompt to tell him that the data has changed ~ ~
3. Thinking
Concurrency management Although the EF helped me do it, but we should not only confine and implement to think about what inspired us in the middle.
Turn on SQL Monitor what EF did for us when we opened the update
EXEC sp_executesql N ' Update [dbo]. [Department]
Set [Name] = @0, [Budget] = @1, [StartDate] = @2, [Instructorid] = @3
where ([DepartmentID] = @4) and (1537709052 = @5))
Select 1537709052
from [dbo]. [Department]
where @ @ROWCOUNT > 0 and [DepartmentID] = @4 ', N ' @0 nvarchar (), @1 decimal (19,4), @2 datetime,@3 int,@4 int,@5 binary (8) ', @0=n ' WLF ', @1=10000.0000,@2= ' 1 12:00:00:000am ', @3=3,@4=3,@5=0x00000000000007d9
We see an update after the query timestamp
So personal speculation is not necessarily the case for the process.
When the user reads this data, it reads to his Timestamp and then the update detects if it is the same as the last time. This method has been updated by someone just now. We can use the same as ADO
Careful friends will find that the above SQL statement update is not updated Timestamp AH
Let's talk about this timestamp here.
Each database has a counter that is incremented when an INSERT or update operation is performed on a table that contains timestamp columns in the database. The counter is a database timestamp. This can track the relative time within the database, not the actual time associated with the clock. A table can have only one timestamp column. The incremental database timestamp value is inserted in the timestamp column each time the row that contains the timestamp column is modified or inserted.
See MSDN------Original
Summarize
Learning should be more to understand the principle of thought to break the stand ~ ~
Next section EF handles inheritance
mvc3+ef4.1 Learning Series (vii) Processing of-----EF concurrency