简体   繁体   中英

Is batch or bulk insert possible in Linq 2 Sql ?

My scenario is little bit wierd, I have a list of entities, say i have ten items in list which will go to three different tables , associated to each other. I am using Linq 2 Sql and I need to insert it in a single hit instead of multiple iterations.

Is this possible. I have heard, in BLtoolkit there is InsertBatch() method that performs a bulk insert. Anything similar in L2S.

There is InsertAllOnSubmit . With Linq To SQL, you just set the properties you want to change for an update. Then calling SubmitChanges will do the rest for you..

In short: No, it is not possible.

InsertAllOnSubmit is basically just calling InsertOnSubmit each time. So that does not help a lot.

If you profile the generated SQL, you will see that you will get a lot of individual insert statements resulting in a lot of overhead. Regardless of using InsertOnSumbit or InsertAllOnSumbit.

If you google around, you will see some attempts to add SqlBulkCopy behaviour to Linq-2-sql. For example: http://blogs.microsoft.co.il/blogs/aviwortzel/archive/2008/05/06/implementing-sqlbulkcopy-in-linq-to-sql.aspx

However, I think you might be better of implementing SqlBulkCopy yourself for your batch jobs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM