c# - Efficient way to edit a list of EF entities -


i know opinion efficient way update entity contains collection of child-entities, using ef. not familiar inner workings of ef.

let's have study entity, has many center entities. 2 tables linked using linking table contains idstudy , idcenter. list of centers belong study can change. every study has between 1 , 50 centers.

for example: initial selection center 1,2,3,4. user edits list removing 1,2 , adding 5,6. new list 3,4,5,6.

what approach more efficient update db?

  1. delete existing entries. inserting items in new list.

  2. determine new. determine should inserted. delete no longer selected. insert new.

  3. for each item in new list, update existing records. delete remaining records if new list has fewer elements. insert rest of elements if new list has more.

thank in advance.

personally go first option. writing code updates if necessary, , deletes right records afterwards, cumbersome.

you can delete pretty entity framework, , can in transaction even. i'd go first option. it's less code, easier read , easier maintain.

of course, if want keep audit trail (what used selected centers before edited?), method not feasible, or you'll have think of other way implement audit trail.

and yes, if list grows quite large (let's 50 records), become slower insert 50 rows. nonwithstanding, code far less contain bugs quite simple. delete centers study. insert new centers study. it's time-saver well.

so in short: if performance isn't absolutely critical, spend milliseconds performing insert operations on database, instead of writing code update/insert changed records.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -