Entity Framework and SqlBulkCopy

.net c# entity-framework sqlbulkcopy

Question

The three typical levels of my current project are data, business, and presentation. For all of my data access requirements, I'd prefer to utilize data entities. The program will need to replicate all of the data from a flat file into a database as part of its functionality. Because the file isn't that large, I can utilize SqlBulkCopy. I've come across a number of articles on how to use the SqlBulkCopy class in.NET. To transfer data back and forth, DataTables are used in every article.

Is it possible to combine SqlBulkCopy with data entities, or would I have to use DataTables instead?

1
16
3/31/2010 2:08:54 PM

Expert Answer

In addition to @DaveHogan's response,

There are now new libraries that enable Bulk Insert for Entity Framework utilizing data entities instead of DataTable (behind the hood, SqlBulkCopy).

I am the project's owner, according to Disclaimer.

Although this library is not free, it makes it simple to:

  • BulkSaveChanges
  • BulkInsert
  • BulkUpdate
  • BulkDelete
  • BulkMerge

In the background, SqlBulkCopy is used. Instead of having to code a unique solution for each Bulk Insert, using the extension technique is more simpler and quicker.

Example

// Easy to use
context.BulkSaveChanges();

// Easy to customize
context.BulkSaveChanges(bulk => bulk.BatchSize = 100);

poor performance

I'm sorry you're not doing well.

Make sure you are not accounting for time lost due to typical errors like failing to JIT Compile or using Add rather than AddRange, which have an influence on test performance but are unrelated to our library.

When removing all frequent performance benchmark errors, most users claim to have seen a performance gain of 25–50 times.

look at Benchmark for Entity Framework Extensions

-3
2/27/2018 2:32:52 PM

Popular Answer

The Entities must be transformed into an IDataReader or DataTable.

There is a little helper class available to aid with: http://archive.msdn.microsoft.com/LinqEntityDataReader/Release/ProjectReleases.aspx?ReleaseId=389

EDIT: The link to the alternative version on MSDN is here: https://github.com/matthewschrager/Repository/blob/master/Repository.EntityFramework/EntityDataReader.cs

Then, you may use SqlBulkCopy as follows:

var sbCopy= new SqlBulkCopy(connectionString);
sbCopy.DestinationTableName = "TableName";
sbCopy.WriteToServer(entitiesList.AsDataReader()); 


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow