c# - Updating n:m relationship without much overhead -


i got n:m relationship between these 3 tables: products, keywords , productkeywords

now everytime update product, provide list<keyword> new chosen keywords. collection can contain keywords chosen product keywords, new or removed ones. want update in database.

for i'm clearing entire productkeywords table productkeywords.productid equals product.id. of course produces very much overhead clears out keywords specific product.

is there easy way update n:m relationship without clearing entirety of products keywords before? should remove productkeywords not present in new list<keyword> collection anymore still in table, add new productkeywords not present in table yet, in list<keyword>. others (present in table , present in list<keyword>) should remain untouched lessen overhead.

  1. it should remove productkeywords not present in new list collection anymore still in table,
  2. as add new productkeywords not present in table yet, in list.

these 2 operations; them separately.

list<keyword> existingkeywordsforproduct = ...; list<keyword> newkeywordsforproduct = ...;  var keywordstoremove = existingkeywordsforproduct.except(newkeywordsforproduct); var keywordstoadd = newkeywordsforproduct.except(existingkeywordsforproduct); 

if existing keywords going reasonably small you'll want pull them list , use linq objects except calls. if list large better keep iqueryable<keyword> , 2 round trips except calls can done on db end.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -