Entity Framework Core implements MySQL TimeStamp/RowVersion concurrency control and entityframework concurrency

Source: Internet
Author: User

Entity Framework Core implements MySQL TimeStamp/RowVersion concurrency control and entityframework concurrency

A problem encountered when migrating a general serial number generator library from SQL Server to Mysql is the implementation of the TimeStamp/RowVersion concurrency control type in a non-Microsoft SQL Server database. The SQL Server timestamp data type is independent of the time and date. SQL Server timestamp is a binary number that indicates the relative sequence of data modifications in the database. The timestamp data type was initially implemented to support the SQL Server restoration algorithm. Each time a page is modified, the current @ DBTS value is used to mark it once, and then @ DBTS is added with 1. This is enough to help the recovery process determine the relative order of page modifications, but the timestamp value has nothing to do with time. In MySQL, the TIMESTAMP column type provides a type that you can use to automatically mark the INSERT or UPDATE operation with the current date and time. If you have multiple TIMESTAMP columns, only the first one is automatically updated.

After IsConcurrencyToken is configured in Entity Framework, RowVersion is automatically used in the where clause to compare Row Version. We also need to use this feature to implement concurrency control, Ak. ini's blog http://www.cnblogs.com/akini/archive/2013/01/30/2882767.html, we solve the concurrency control problem on Entity framework core according to the method in this article.

Type of the serial number defined:

[Table ("DbServerSequence")]
Public class DbServerSequence: ISequence
{

Public DbServerSequence ()
{

}
Public DbServerSequence (SequenceOptions options): this ()
{
StartAt = options. StartAt;
CurrentValue = StartAt;
Increment = options. Increment;
MaxValue = options. MaxValue;
MinValue = options. MinValue;
Cycle = options. Cycle;

}

Public String Key {get; set ;}
Public long StartAt {get; set ;}
Public int Increment {get; set ;}
Public long MaxValue {get; set ;}
Public long MinValue {get; set ;}
Public bool Cycle {get; set ;}
Public long CurrentValue {get; set ;}

[ConcurrencyCheck]
Public DateTime RowVersion {get; set ;}

Public DateTime DateCreated {get; set ;}
}
RowVersion is used for concurrency control. For Mysql, TimeStamp/RowVersion is not allowed for byte [] type. DateTime type is used here.

The database table is defined as follows (since MySQL 5.6.5, the DEFAULT CURRENT_TIMESTAMP and on update CURRENT_TIMESTAMP options can also be applied to Datetime columns ):

Drop table if exists 'dbserversequence ';
Create table 'dbserversequence '(
'Key' varchar (128) not null,
'Startat' bigint (20) not null,
'Credentials' int (11) not null,
'Maxvalue' bigint (20) not null,
'Minvalue' bigint (20) not null,
'Cycle' bit (1) not null,
'Currentvalue' bigint (20) not null,
'Rowversion' datetime not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
'Datecreated 'datetime not null,
Primary key ('key ')
) ENGINE = InnoDB default charset = utf8;

The OnModelCreating in SequenceDbContext is rewritten as follows, mainly to configure the concurrency control field:

Protected override void OnModelCreating (ModelBuilder builder)
{
Base. OnModelCreating (builder );

Builder. Entity <DbServerSequence> (e =>
{
E. HasKey (x => x. Key );
E. Property (x => x. RowVersion). IsRowVersion (). IsConcurrencyToken ();
});
}
This solution also applies to various databases, especially databases similar to MySql and Postgresql that do not support the default RowVersion field. The latest code put in https://github.com/geffzhang/Sequence/tree/dotnetcore

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.