Referencing object's identity before submitting changes in LINQ
Posted
by Axarydax
on Stack Overflow
See other posts from Stack Overflow
or by Axarydax
Published on 2009-12-09T16:41:58Z
Indexed on
2010/03/16
10:46 UTC
Read the original article
Hit count: 316
Hi, is there a way of knowing ID of identity column of record inserted via InsertOnSubmit
beforehand, e.g. before calling datasource's SubmitChanges
?
Imagine I'm populating some kind of hierarchy in the database, but I wouldn't want to submit changes on each recursive call of each child node (e.g. if I had Directories table and Files table and am recreating my filesystem structure in the database).
I'd like to do it that way, so I create a Directory object, set its name and attributes,
then InsertOnSubmit
it into DataContext.Directories collection, then reference Directory.ID in its child Files. Currently I need to call InsertOnSubmit to insert the 'directory' into the database and the database mapping fills its ID column. But this creates a lot of transactions and accesses to database and I imagine that if I did this inserting in a batch, the performance would be better.
What I'd like to do is to somehow use Directory.ID before commiting changes, create all my File and Directory objects in advance and then do a big submit that puts all stuff into database. I'm also open to solving this problem via a stored procedure, I assume the performance would be even better if all operations would be done directly in the database.
© Stack Overflow or respective owner