{ Dev Farm }

Web & Windows Development

Speed up Entity Framework large data inserting with SqlBulkCopy

| 0 commenti

5.00 avg. rating (95% score) - 2 votes

As known Entity Framework is quite slow when inserting items and almost unusable when you are insertin 1000+ items.
A very fast workaround is using SqlBulkCopy, it’s very fast and easy to use.

public static void saveData<T>(ref List<T> list, string destinationTableName, int batchSize)
	using (EntityDataReader<T> reader = new EntityDataReader<T>(list))
	using (System.Data.SqlClient.SqlBulkCopy sbc = new System.Data.SqlClient.SqlBulkCopy(PhoneLog.Core.Configurations.ConnectionString))
		for (int i = 0; i < reader.FieldCount; i++)
			string colName = reader.GetName(i);
			sbc.ColumnMappings.Add(colName, colName);
		sbc.BatchSize = batchSize;
		sbc.DestinationTableName = destinationTableName;

In this sample T should be a EntityFramework known object.

Lascia un commento

I campi obbligatori sono contrassegnati con *.

:bye:  :good:  :negative:  :scratch:  :wacko:  :yahoo:  B-)