简体   繁体   English

在Linq 2 Sql中可以批量或批量插入吗?

[英]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. 我正在使用Linq 2 Sql,我需要将其插入一次命中,而不是多次迭代。

Is this possible. 这可能吗。 I have heard, in BLtoolkit there is InsertBatch() method that performs a bulk insert. 我听说,在BLtoolkit中,有一个执行批量插入的InsertBatch()方法。 Anything similar in L2S. L2S中的任何相似之处。

There is InsertAllOnSubmit . InsertAllOnSubmit With Linq To SQL, you just set the properties you want to change for an update. 使用Linq To SQL,您只需设置要更改的属性即可进行更新。 Then calling SubmitChanges will do the rest for you.. 然后调用SubmitChanges将为您完成其余工作。

In short: No, it is not possible. 简而言之:不,这是不可能的。

InsertAllOnSubmit is basically just calling InsertOnSubmit each time. 基本上,InsertAllOnSubmit每次只是调用InsertInSubmit。 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. 如果您对生成的SQL进行概要分析,则会看到您将获得很多单独的插入语句,从而导致大量开销。 Regardless of using InsertOnSumbit or InsertAllOnSumbit. 无论使用InsertInSumbit还是InsertAllOnSumbit。

If you google around, you will see some attempts to add SqlBulkCopy behaviour to Linq-2-sql. 如果您四处搜索,您将看到一些尝试将SqlBulkCopy行为添加到Linq-2-sql。 For example: http://blogs.microsoft.co.il/blogs/aviwortzel/archive/2008/05/06/implementing-sqlbulkcopy-in-linq-to-sql.aspx 例如: 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. 但是,我认为您最好为批处理作业自己实现SqlBulkCopy。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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