Home > Net >  About EF line version timestamp
About EF line version timestamp

Time:03-20

When handling concurrency conflict in EF, I adopted the line version RowVersion,
However, total feel a bit bad to use, such as one of the modified the database data, the update after submission, the client must retrieve the data again, otherwise how to continuously change, will be an error, because read only once, but modify 2 times, the second is an error,

The great god, you have what good way?

CodePudding user response:

RowVersion your RowVersion wrongly, should be to the client, rather than to the database since the incremental,

CodePudding user response:

My RowVersion
 protected override void OnModelCreating (DbModelBuilder modelBuilder) 
{
//call the superclass method
Base. OnModelCreating (modelBuilder);

//will be excluded from the EF TrackingState fields change tracking function
ModelBuilder. Types (). The Configure (x=& gt; X.I gnore (y=& gt; Y.T rackingState));

//register to concurrent control model table
//ConcurrencyTypes. Add (typeof (sys_user));
//modelBuilder. Entity
//a custom function registered database
//for EF's own DbFunctions implementation is actually the same function, so don't need to pass the EFDbFunctions system function definition, but this code still have reference value, when need to use a custom function still need to use
ModelBuilder. Conventions. The Add (new FunctionsConvention (" ", typeof (ass_EFDbFunctions)));
}


 public override int SaveChanges () 
{
This. ChangeTracker. DetectChanges ();
Var entries=this. ChangeTracker. Entries ();

//modify data tracking and concurrent control
Foreach (var entry in the entries)
{
//only new or modified, the need to track and concurrency control
If (entry State!=EntityState. Added & amp; & Entry. The State!=EntityState. Modified) continue;

Var obj=entry. The Entity;

//data change tracking record
If (entry. State==EntityState. Added)//record data creation time
{
If (obj. HasProperty (" CreateTime ")). Obj SetPropertyValue (" CreateTime ", a DateTime. Now);
}
Else if (entry. State==EntityState. Modified)//record data modification time
{
If (obj. HasProperty (" ModifyTime ")). Obj SetPropertyValue (" ModifyTime ", a DateTime. Now);

//because CreateTime may not be extracted by client, so the client upload data is also a lack of CreateTime, so update need to eliminate, thus preventing is updated to null
Entry. The Property (" CreateTime "). IsModified=false;
}
//obj. TrackInfo="admin";//record tracking information

//concurrency control
If (concurrencyTypes. Count & gt; 0 & amp; & ConcurrencyTypes. The Contains (obj. GetType ()) & amp; & Obj. HasProperty (" RowVersion "))
{
//set to write the value of the new (including new and modified, all want to write a new GUID)
Obj. SetPropertyValue (" RowVersion, "System. Text. Encoding. UTF8. GetBytes (Guid. NewGuid (). The ToString ()));
}
}

Return the base. The SaveChanges ();
}

CodePudding user response:

In.net that haven't a piece of subsoil,
It is not hard to see from your description, however, this is an optimistic locking, cas and use by the version number,

Since is the cas, so when reading records, there is a specific version number,
And your data submitted to modify, is the client behavior,
The reason why you submit twice, to query the database,
Because you don't keep submitted after the completion of the new version number for the first time,
A rough estimate, still use the old version,

CodePudding user response:

refer to the second floor of the knights templar 18 response:
my RowVersion

Look not to understand your code very difficult oh, ah, EF use line version to handle concurrency, is not automatic, how do you RowVersion is the client? The client how to produce? .
  •  Tags:  
  • C#
  • Related