Whats the proper way to do explicit transactions with linq to sql? -


i have scenarios need have multiple calls .submitchanges() on datacontext, want explicitly control transaction myself make atomic. while have been doing creating connection, creating transaction on connection, creating datacontext , passing both. lets assume dont want use transactionscope instead. code looks this:

using conn new sqlconnection("connection string...")    conn.open()    using trans = conn.begintransaction()       dim dc new datacontext(conn)       dc.transaction = trans        ' work         trans.commit()    end using end using 

i began using linq sql profiler , breaks code. reason require use .connection property on datacontext create transaction. fails if use connection variable directly (which think silly). question is, more appropriate way:

using conn new sqlconnection("connection string...")    conn.open()    dim dc new datacontext(conn)    using trans = dc.connection.begintransaction()       dc.transaction = trans        ' work         trans.commit()    end using end using 

which more accepted way this?

the second snippet doesn't seem appropriate me. second snippet need create transaction after creating context, -at least- readability / maintainability perspective less useful. try imagine how code when need create 2 datacontext classes, , create transaction (only) after creating first context. makes pretty hard keep clean separated code.

i think should send mail hibernating rhinos , ask if fix bug.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -