c# - Linq To SQL Delete + Insert best practice -
as stated in title need perform delete + insert, :
context.deleteallonsubmit ( deletequery ) ; foreach ( var entry in entries ) contex.insertonsubmit ( entry ) ; context.submitchanges();
as wrote in post : linq sql: execution order when calling submitchanges()
i read delete operation last 1 applied, @ moment see logic work (i sure delete+insert happen dozen of times per day). need understand if post wrong or logic , reason (update check flag in linq sql datamodel?) lucky , avoid trouble.
after know better pattern make "update" when record cardinality changes. mean in table there primary key identify entity (an entity has many records) , subkey identify each record in same entity (sub entity). need regenerate (because sub entity may inserted, edited or delete) use delete + insert (in messagge form write db contains entity , sub enetity exist, not deleted ones).
eg:
id subid data 1 1_0 father 2 2_0 father 2 2_1 child 1 3 3_0 father 3 3_1 child 1 3 3_2 child 2
i have no control nor on table (and data format inside them) nor on message (that use write or delete table displaied above).
i read delete operation last 1 applied, @ moment see logic work (i sure delete+insert happen dozen of times per day). i need understand if post wrong or logic is , reason (update check flag in linq sql datamodel?) lucky , avoid trouble.
post correct, delete deleted @ last.
your code working per design, not chance.
it loads records deleted , deleted 1 one. happens @ last.
this never fail or not deleted wrong records, has performance issue, can refer msdn article on this
regardless of how many changes make objects, changes made in-memory replicas. have made no changes actual data in database. changes not transmitted server until explicitly call submitchanges on datacontext.
when make call, datacontext tries translate changes equivalent sql commands. can use own custom logic override these actions, order of submission orchestrated service of datacontext known change processor.
the sequence of events follows: refer msdn
when call submitchanges, linq sql examines set of known objects determine whether new instances have been attached them. if have, these new instances added set of tracked objects. this why saying insertion @ first
all objects have pending changes ordered sequence of objects based on dependencies between them. objects changes depend on other objects sequenced after dependencies. then update
after update deletion done
immediately before actual changes transmitted, linq sql starts transaction encapsulate series of individual commands.
the changes objects translated 1 one sql commands , sent server.
at point, errors detected database cause submission process stop, , exception raised.
all changes database rolled if no submissions ever occurred. the datacontext still has full recording of changes
Comments
Post a Comment