c# - Updating EF entities based on deep JSON data -


i have data structure looks this: foo 1:* bar 1:* baz

it when passed client:

{     id: 1,     bar: [{             id: 1,             baz: []         },         {             id: 2,             baz: [{                 id: 1             }]         }     ] } 

in ui, represented tree structure, user can update/add/remove items on levels.

my question is, when user has made modifications , i'm sending altered data server, how should perform ef database update? initial thought implement dirty tracking on client side, , make use of dirty flag on server in order know update. or maybe ef can smart enough incremental update itself?

unfortunately ef provides little if no such scenario.

the change tracker works in connected scenarios, working disconnected entities has been totally left out develpers using ef. provided context methods manipulating entity state can handle simplified scenarios primitive data, not play related data.

the general way handle load existing data (icluding related) database, detect , apply add/updates/deletes yourself. accounting related data (navigation property) types (one-to-many (owned), many-to-one (associated), many-to-many etc), plus hard way work ef6 metadata makes generic solution extremely hard.

the attempt address issue generically afaik graphdiff package. applying modifications package in scenario simple that:

using refactorthis.graphdiff;  ienumerable<foo> foos = ...; using (var db = new yourdbcontext()) {     foreach (var foo in foos)     {         db.updategraph(foo, foomap =>             foomap.ownedcollection(f => f.bars, barmap =>                 barmap.ownedcollection(b => b.bazs)));     }     db.savechanges(); } 

see introducing graphdiff entity framework code first - allowing automated updates of graph of detached entities more info problem , how package addressing different aspects of it.

the drawback package no more supported author, , there no support ef core in case decide port ef6 (working disconnected entities in ef core has improvements, still doesn't offer general out of box solution).

but implementing correctly update manually specific model real pain. comparison, condensed equivalent of above updategraph method 3 simple entities having primitive , collection type navigation properties this:

db.configuration.autodetectchangesenabled = false; var fooids = foos.where(f => f.id != 0).select(f => f.id).tolist(); var oldfoos = db.foos     .where(f => fooids.contains(f.id))     .include(f => f.bars.select(b => b.bazs))     .todictionary(f => f.id); foreach (var foo in foos) {     foo dbfoo;     if (!oldfoos.trygetvalue(foo.id, out dbfoo))     {         dbfoo = db.foos.create();         dbfoo.bars = new list<bar>();         db.foos.add(dbfoo);     }     db.entry(dbfoo).currentvalues.setvalues(foo);     var oldbars = dbfoo.bars.todictionary(b => b.id);     foreach (var bar in foo.bars)     {         bar dbbar;         if (!oldbars.trygetvalue(bar.id, out dbbar))         {             dbbar = db.bars.create();             dbbar.bazs = new list<baz>();             db.bars.add(dbbar);             dbfoo.bars.add(dbbar);         }         else         {             oldbars.remove(bar.id);         }         db.entry(dbbar).currentvalues.setvalues(bar);         var oldbazs = dbbar.bazs.todictionary(b => b.id);         foreach (var baz in bar.bazs)         {             baz dbbaz;             if (!oldbazs.trygetvalue(baz.id, out dbbaz))             {                 dbbaz = db.bazs.create();                 db.bazs.add(dbbaz);                 dbbar.bazs.add(dbbaz);             }             else             {                 oldbazs.remove(baz.id);             }             db.entry(dbbaz).currentvalues.setvalues(baz);         }         db.bazs.removerange(oldbazs.values);     }     db.bars.removerange(oldbars.values); } db.configuration.autodetectchangesenabled = true; 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -