When doing projects, we usually have several processing modes for optimistic concurrency
1. Tell the user that the data has been changed by someone else. You'll have to start again.
2. Cover the data directly, I'm the biggest.
3. Use the data of the person.
Here's how code first is done.
[Table ("Product")][jsonobject (isreference=true)] Public classproduct{[Key] Public intID {Get;Set; } Public stringCode {Get;Set; } PublicDateTime dateCreated {Get;Set; } [Concurrencycheck]//Key PublicDateTime DateModified {Get;Set; } Public VirtualList<category> Categories {Get;Set; } Public Virtuallist<color> Colors {Get;Set; }}
MySQL has to have this column.
ALTER TABLE ' db '. ' Product ' ADD COLUMN ' datemodified ' TIMESTAMP not NULL DEFAULT current_timestamp on UPDATE current_timest AMP;
using(DB db =NewDB ()) { varProduct = Db.products.Find (1); Product.code="mk99"; Try{db. SaveChanges (); } Catch(Dbupdateconcurrencyexception ex)//dbupdateconcurrencyexception { varObjcontext =((iobjectcontextadapter) db). ObjectContext; varEntry =Ex. Entries.single (); //Objcontext.refresh (refreshmode.clientwins, entry. Entity);Objcontext.refresh (refreshmode.storewins, entry. Entity); Db. SaveChanges (); } }
Catch the exception with Dbupdateconcurrencyexception, and then we can look at the various data of this entry to define our handling.
Objcontext.refresh is a Microsoft-provided solution, Refreshmode.storewins is to replace local data with the latest data from the current database, Clientwins is to overwrite the local data into the database.
Entity Framework with MySQL learning Note one (optimistic concurrency)