Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the nextgen-gallery-pro domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/mxvysmpn/public_html/wp-includes/functions.php on line 6121
Speed up Entity Framework large data inserting with SqlBulkCopy | Speed up Entity Framework large data inserting with SqlBulkCopy – { Dev Farm }

{ 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;
		sbc.WriteToServer(reader);
	}
}

In this sample T should be a EntityFramework known object.

Lascia un commento

I campi obbligatori sono contrassegnati con *.