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 开发者_Python百科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.
精彩评论