c# - How can I avoid unnecessary concurrency exceptions in my EF 6 scenario? -
i using ef 6. have 2 tier application using wcf soap , dtos transfer data. send object graph across wire user update, , send persist changes. in between, use automapper convert , dtos , entities.
on persistence side, receive dto , unpack (it contains lists of entities). unpack appropriate entities , check whether adding or editing checking if id 0. far have blindly updated every entity id > 0.
however, have learned not best way because throws unnecessary concurrency exceptions. if call update on entity, rowversion gets updated, if no fields changed values in database. causing unnecessary concurrency exceptions. need avoid these. need somehow check see if entity needs updated or not. way know how check whether entity needs updated, query database , property property check.
my question: on right track? in fact need check entity property property find out if needs updated? there easier or more automatic way?
i feel frustrated need query database each entity before saving changes. maybe missing something?
here sample of code:
var ws = saverequest.workspace; var td = saverequest.todelete; using (var db = new engineeringcontext()) { var ass = attach<jobassembly>(db, db.jobassemblies, ws.assemblies.todomain()); var opr = attach<joboperation>(db, db.joboperations, ws.operations.todomain()); var mtl = attach<jobmaterial>(db, db.jobmaterials, ws.materials.todomain()); delete<jobassembly>(db, td.assemblies.todomain()); delete<joboperation>(db, td.operations.todomain()); delete<jobmaterial>(db, td.materials.todomain()); try { db.savechanges(); } catch (dbupdateconcurrencyexception ex) { throw new exception("sync error: data failed save due changes user.", ex); } ws.assemblies = ass.todto(); ws.operations = opr.todto(); ws.materials = mtl.todto(); }
where attach , delete this:
db.entry<t>(entity).state = entitystate.modified; // .deleted deletes.
and todomain , todto extensions map domains (entities) dtos , viceversa.
Comments
Post a Comment