C# 如何进行批量插入——Linq to Entities
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1609153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do a Bulk Insert -- Linq to Entities
提问by Luke101
I cannot find any examples on how to do a Bulk/batch insert using Linq to Entities. Do you guys know how to do a Bulk Insert?
我找不到任何关于如何使用 Linq to Entities 进行批量/批量插入的示例。你们知道如何进行批量插入吗?
采纳答案by Marc Gravell
Sometimes you simply have to mix models. Perhaps use SqlBulkCopy
for this part of your repository (since this plugs directly into the bulk-copy API), and Entity Framework for some of the rest. And if necessary, a bit of direct ADO.NET. Ultimately the goal is to get the job done.
有时您只需要混合模型。也许SqlBulkCopy
用于存储库的这一部分(因为它直接插入批量复制 API),而实体框架用于其余部分。如有必要,直接使用 ADO.NET 位。最终目标是完成工作。
回答by Contango
For a perfect example of how to do bulk inserts with LINQ to Entities, see http://archive.msdn.microsoft.com/LinqEntityDataReader. It is a wrapper that allows easy use of SqlBulkCopy.
有关如何使用 LINQ to Entities 进行批量插入的完美示例,请参阅http://archive.msdn.microsoft.com/LinqEntityDataReader。它是一个包装器,可以轻松使用 SqlBulkCopy。
@Marc Gravell is correct, sometimes you have to mix models to get the job done.
@Marc Gravell 是正确的,有时您必须混合模型才能完成工作。
回答by Ronnie Overby
I wrote a class that will bulk insert EF entities (or any kind of object as long as the property names match the column names).
我编写了一个类,它将批量插入 EF 实体(或任何类型的对象,只要属性名称与列名称匹配)。
The class supports customizing the batch size, pre & post insert events, queued inserts, and "firehose mode" (give it a billion objects, it will respect the batch size).
该类支持自定义批量大小、前后插入事件、排队插入和“消防模式”(给它十亿个对象,它将尊重批量大小)。
- The code:
BulkInserter<T>
- How to use it
回答by Amit Bisht
For inserting a huge amount of data in a database, I used to collect all the inserting information into a list and convert this list into a DataTable
. I then insert that list to a database via SqlBulkCopy
.
为了在数据库中插入大量数据,我曾经将所有插入信息收集到一个列表中,并将该列表转换为DataTable
. 然后我将该列表插入到数据库中SqlBulkCopy
。
Where I send my generated list LiMyList
which contain information of all bulk data which I want to insert to database
and pass it to my bulk insertion operation
我在哪里发送我生成的列表LiMyList
,其中包含我想要插入到数据库的所有批量数据的信息并将其
传递给我的批量插入操作
InsertData(LiMyList, "MyTable");
Where InsertData
is
哪里InsertData
是
public static void InsertData<T>(List<T> list,string TabelName)
{
DataTable dt = new DataTable("MyTable");
clsBulkOperation blk = new clsBulkOperation();
dt = ConvertToDataTable(list);
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
using (SqlBulkCopy bulkcopy = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["SchoolSoulDataEntitiesForReport"].ConnectionString))
{
bulkcopy.BulkCopyTimeout = 660;
bulkcopy.DestinationTableName = TabelName;
bulkcopy.WriteToServer(dt);
}
}
public static DataTable ConvertToDataTable<T>(IList<T> data)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
foreach (T item in data)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
return table;
}