sql - How to effectively refresh many to many relationship -
lets have entity a, have many many relationship entities of type a. on entity a, have collection of a. , lets have "update" relationships according external service - time time receive notification relations entity has changed, , array of ids of current related entities - relations can new, existing, of existing no longer there... how can update database ef ? ideas:
eager load entity related entities, foreach on collection of ids external service, , remove/add needed. not effective - need load possibly hundreds of related entities
clear current relations , insert new. how ? maybe perform delete stored procedure, , insert "fake" objects
a.related.add(new { id = idfromarray })
but can done in transaction ? (call stored procedure , inserts done savechanges
)
or there 3rd way ?
thanx.
well, "from time time" not sound situation think performance improvement (unless mean "from millisecond millisecond") :)
anyway, first approach correct idea update without stored procedure. , yes, must load old related entities because updating many-to-many relationship goes though efs change detection. there no exposed foreign key leverage update relations without having loaded navigation properties.
an example how might in detail here (fresh question yesterday):
selecting & updating many-to-many in entity framework 4
(only last code snippet before "edit" section relevant question , edit section itself.)
for second solution can wrap whole operation manually created transaction:
using (var scope = new transactionscope()) { using (var context = new mycontext()) { // ... call stored procedure delete relationships in link table // ... insert fake objects new relationships context.savechanges(); } scope.complete(); }
Comments
Post a Comment