Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I ran into this problem trying to insert 50,000+ records into a SQL database using entity framework. The entity framework is not meant for huge bulk operations (large insert or delete operations) so I ended up using the System.Data.SqlClient.SqlBulkCopy library, which is much more efficient and faster. I even wrote the below helper function to auto-map so I didn't have to manually construct a SQL Insert statement. (it's marginally type independent! I think).</p> <p>Basically the workflow is: IList&lt;MyEntityType&gt; -> DataTable -> SqlBulkCopy</p> <pre><code>public static void BulkInsert&lt;T&gt;(string connection, string tableName, IList&lt;T&gt; list) { using (var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepNulls)) { bulkCopy.BatchSize = list.Count; bulkCopy.DestinationTableName = tableName; bulkCopy.BulkCopyTimeout = 3000; var table = new DataTable(); var props = TypeDescriptor.GetProperties(typeof(T)) //Dirty hack to make sure we only have system data types //i.e. filter out the relationships/collections .Cast&lt;PropertyDescriptor&gt;() .Where(propertyInfo =&gt; propertyInfo.PropertyType.Namespace.Equals("System")) .ToArray(); foreach (var propertyInfo in props) { bulkCopy.ColumnMappings.Add(propertyInfo.Name, propertyInfo.Name); table.Columns.Add(propertyInfo.Name, Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType); } var values = new object[props.Length]; foreach (var item in list) { for (var i = 0; i &lt; values.Length; i++) { values[i] = props[i].GetValue(item); } table.Rows.Add(values); } bulkCopy.WriteToServer(table); } } </code></pre> <p>In my example I went from 15-20 minutes to insert to under a minute.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload