SQLBulk Copy With Related Entities (Entity Framework)

.net c# entity-framework sqlbulkcopy sql-server-2008

Question

Say I have the below entities. (Heavily Simplified for brevity, but the key properties are included)

public  class Crime 
{

       [Key]
       public int CrimeId {get;set;}
       public virtual ICollection<Victim> Victims {get;set;}
       public virtual CrimeDetail Detail {get;set}

}

public class Victim
{
       [Key]
       public int VictimId {get;set;}
       public string VictimCategory {get;set;}
}

public class CrimeDetail
{
       [Key]
       public int id {get;set;}
       public string DetailText {get;set;}
}

I have 600,000+ of these records to insert into SQL Server 2008 Express R2, which takes quite some time using Entity Framework 4.4.

Ideally I'd like to use SQLBulkCopy to batch insert these records, but since that doesn't support complex types (At least not out of the box), then I'm trying to find some other potential solutions.

I'm open to changing my model if necessary.

EDIT: would the AsDataReader Extension method from MSDN help in this instance?

Popular Answer

When having the same issue we end up having code-first generated database with EF and strongly typed generated datasets to be used for SQLBulkCopy.

(We never really coded those classes, they were generated using xsd util from xsd definition of 1-10gb xml file. I'm trying to recall right now when we havent generated typed datasets from the same xsd, but that seems irrelevant to your issue.)

Depending on how you are getting those 600k+ records you either can change the code to use generated strongly-typed datasets or use some object-to-object mapper to map your EF POCO objects to datasets as properties going to be named the same.

Here is a link on generating strongly typed datasets.

Here is an example how to use SqlBulkInsert.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why