{ Dev Farm }

Web & Windows Development

Speed up Entity Framework large data inserting with SqlBulkCopy

| 0 commenti

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(ref List list, string destinationTableName, int batchSize)
	using (EntityDataReader reader = new EntityDataReader(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 *.