c# - Generic DbUpdateConcurrencyException Handler EF Core 2.0 -
i have working solution handling dbupdateconcurrencyexception user can presented values exist in database compared own posted input when concurrency token (byte[] rowversion) doesn't match.
catch (dbupdateconcurrencyexception ex) { var entry = ex.entries.single(); var clientvalues = (applicationuser)entry.entity; var databaseentry = entry.getdatabasevalues(); if (databaseentry == null) { modelstate.addmodelerror(string.empty, "unable save changes. subject user deleted user."); } else { var databasevalues = (applicationuser)databaseentry.toobject(); if (databasevalues.firstmidname != clientvalues.firstmidname) modelstate.addmodelerror("firstmidname", "current value: " + databasevalues.firstmidname); if (databasevalues.lastname != clientvalues.lastname) modelstate.addmodelerror("lastname", "current value: " + databasevalues.lastname); // etc.. every property can changed modelstate.addmodelerror(string.empty, "the record attempted edit " + "was modified user after got original value. " + "edit operation canceled , current values in database " + "have been displayed. if still want edit record, click " + "the save button again. otherwise click list hyperlink."); requestmodel.rowversion = databasevalues.rowversion; modelstate.remove("rowversion"); } }
i can see kind of concurrency handling being used on of edit actions across project i've been trying come generic solution.
is possible handle exception entity , iterate on properties perform value comparison between posted values , database values without needing explicitly declare entity or property names?
this (wishful thinking) example of i've been trying achieve can't property info of available entities:
var entry = ex.entries.single(); var clientvalues = entry.entity; var databaseentry = entry.getdatabasevalues(); var databasevalues = databaseentry.toobject(); foreach(var property in databasevalues.metadata.properties) { var dbvalue = databasevalues.property(property.name).currentvalue; var clientvalue = clientvalues.property(property.name).currentvalue if(dbvalue != clientvalue) modelstate.addmodelerror(property.name, "current value: " + dbvalue); }
Comments
Post a Comment