Luke101
Luke101

Reputation: 65238

How to do a Bulk Insert -- Linq to Entities

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?

Upvotes: 36

Views: 37541

Answers (4)

Amit Bisht
Amit Bisht

Reputation: 5136

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.

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

InsertData(LiMyList, "MyTable");

Where InsertData is

 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;
        }

Upvotes: 2

Ronnie Overby
Ronnie Overby

Reputation: 46470

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).

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).

Upvotes: 6

Contango
Contango

Reputation: 80242

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.

@Marc Gravell is correct, sometimes you have to mix models to get the job done.

Upvotes: 12

Marc Gravell
Marc Gravell

Reputation: 1062650

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.

Upvotes: 22

Related Questions