Click here to Skip to main content
11,930,788 members (56,696 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked

Using SQL Bulk Copy with your LINQ-to-Entities datacontext - Part 1

, 27 Aug 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This tip shows how to significantly speed up inserts of large amounts of data using the entity framework.


Linq-to-Entities is great for many things but it doesn't do very well with huge amounts of inserts. Extending the data context partial class with the following though will do the trick. All you need to do is to create your LINQ entities just as you usually do and call the BulkInsertAll method on the datacontext. Also, do not use this datacontext for anything else before doing the bulk insert. The connection string of the datacontext that we use to create the SQLConnection here loses its password after its first use for some reason and that makes it, obviously, a bit difficult to connect. This has also been posted on my blog. If you are using POCO objects and EF5 take a look at this

public partial class MyEntities
    partial void OnContextCreated()
        CommandTimeout = 5 * 60;

    public void BulkInsertAll<t>(IEnumerable<t> entities)
        entities = entities.ToArray();
        var ec = (EntityConnection) Connection; 
        var conn = (SqlConnection) ec.StoreConnection;
        Type t = typeof(T);

        var entityTypeAttribute = (EdmEntityTypeAttribute)t.GetCustomAttributes
        (typeof(EdmEntityTypeAttribute), false).Single();
        var bulkCopy = new SqlBulkCopy(conn) { DestinationTableName = entityTypeAttribute.Name };

        var properties = t.GetProperties().Where(EventTypeFilter).ToArray();
        var table = new DataTable();

        foreach (var property in properties)
            Type propertyType = property.PropertyType;
            if (propertyType.IsGenericType &&
                propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                propertyType = Nullable.GetUnderlyingType(propertyType);

            table.Columns.Add(new DataColumn(property.Name, propertyType));
            bulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(property.Name, property.Name));

        foreach (var entity in entities)
            var e = entity;
            table.Rows.Add(properties.Select(property => 
            GetPropertyValue(property.GetValue(e, null))).ToArray());


    private bool EventTypeFilter(System.Reflection.PropertyInfo p)
        var attribute = Attribute.GetCustomAttribute(p, 
        typeof(EdmScalarPropertyAttribute)) as EdmScalarPropertyAttribute;

        if (attribute != null) return true;

        return false;

    private object GetPropertyValue(object o)
        if (o == null)
            return DBNull.Value;
        return o;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 28 Aug 2013
Article Copyright 2013 by Måns Tånneryd
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid