No need to modify entities and configurations, use concurrency control consistent with SQL Server in MySQL. Modifying the rowversion type is not advisable, and modifying to timestamp is not feasible. SQL Server rowversion generates a string of unique binary guaranteed row versions, regardless of the timestamp, regardless of the timestamp accuracy problem. Using a mysql trigger only resolves the insertion defaults and updated random values of the UUID, and because of MySQL's own policy to prevent infinite recursion, its triggers cannot update the current table in the trigger of the current table. Therefore, the trigger cannot implement the update value of the RowVersion field that is generated by the database in SQL Server. So rowversion in MySQL can only be assigned by the application.
RowVersion is automatically used in the WHERE clause to compare row Version in EF with Isconcurrencytoken configuration. By overriding the SaveChanges method to set the value of rowversion each time it is added and updated, the current version of row version and the purpose of updating row version are compared at the same time, and the updated row version value can be retrieved correctly.
1. Defining Concurrency control fields
Interface irowversion { set;} }
2. Configure the Concurrency control field
void Onmodelcreating (Dbmodelbuilder modelBuilder) { modelbuilder.conventions.remove< Pluralizingtablenameconvention> (); ModelBuilder.Configurations.AddFromAssembly (typeof (Mysqldbcontext). Assembly); typeof (Irowversion). IsAssignableFrom (o.declaringtype) &&o.propertytype==typeof (byte[]) &&o.name==" RowVersion "). Configure (o = O.isconcurrencytoken (). Hasdatabasegeneratedoption (Databasegeneratedoption.none)); Database.setinitializer (new Mysqldbinitializer ());}
3. Manually assign a value to the rowversion
PublicOverrideint SaveChanges () { This. Changetracker.detectchanges (); var objectContext = ((Iobjectcontextadapter)This). ObjectContext; foreach (objectstateentry entry in objectContext.ObjectStateManager.GetObjectStateEntries ( entitystate.modified | entitystate.added)) { var v = entry. Entity as irowversion; if (v! = null) { v.rowversion = System.Text.Encoding.UTF8.GetBytes (Guid.newgui D (). ToString ()); } } return base. SaveChanges ();}
4. Check the generated SQL statement
UPDATE ' Customer ' SET ' phonenumber ' [email protected], ' RowVersion ' [email protected] WHERE (' Id ' = 1) and (' RowVersion ' = @gp3)--@gp1: ' 635655975120384389 ' (Type = String, isnullable = false, S ize = + )--@gp2: ' system.byte[] ' (Type = Object, isnullable = false, Size = $)--@gp3: ' Sy Stem. Byte[] ' (Type = Object, isnullable = false, Size = $ )
5. View rowversion in the data
6. Prepare the test code
Publicstatic void Test () { var db1 = GetContext (); var customer1 = db1. Set<customer> (). FirstOrDefault (); Customer1. Phonenumber= "t1"; using (var db2 = GetContext ()) { var customer2 = DB2. Set<customer> (). FirstOrDefault (); Customer2. PhoneNumber = "t2"; DB2. SaveChanges (); } DB1. SaveChanges ();}
7. View the test results:
Summarize:
1. Build support with a unique version number, supported by SQL Server (Compact) itself, and MySQL's UUID function.
2. You need to set the rowversion default value and update the rowversion version number when you insert, and the SQL Server (Compact) itself supports that MySQL only supports default values and Automatic updates for timestamp that cannot be used for rowversion. Therefore, you can only set row Version in the app in MySQL.
Non-existent disadvantages:
1.ASP. NET slow (no options for IIS application initialization and recycle configuration are set)
2.Entity Framework slow (not set per request one dbcontext and generate views)
3.Entity framework loads too much data (incorrect use of automapper and lazy loading)
The 4.Entity framework does not require irepository (the value of IRepository is implemented in testing and isolation)
Entity Framework 6 Code first series: No need to modify entities and configurations-use concurrency control consistent with SQL Server in MySQL