Entity Framework with MySQL learning Note one (optimistic concurrency)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.