简体   繁体   English

具有相关实体的SQLBulk Copy(实体框架)

[英]SQLBulk Copy With Related Entities (Entity Framework)

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. 我有600,000多个这些记录要插入SQL Server 2008 Express R2,使用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. 理想情况下,我想使用SQLBulkCopy批量插入这些记录,但是由于它不支持复杂类型(至少不是开箱即用的),因此我正在尝试寻找其他可能的解决方案。

I'm open to changing my model if necessary. 我愿意在必要时更改我的模型。

EDIT: would the AsDataReader Extension method from MSDN help in this instance? 编辑:在这种情况下, 来自MSDNAsDataReader Extension方法有所帮助吗?

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. 当遇到相同的问题时,我们最终将获得具有EF的代码优先生成的数据库以及用于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.) (我们从未真正编码过这些类,它们是使用1-10gb xml文件的xsd定义中的xsd util生成的。我现在想回想一下当我们还没有从同一个xsd生成类型化数据集时,但这似乎与您的问题无关)

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. 根据获得这600k条以上记录的方式,您可以更改代码以使用生成的强类型数据集,也可以使用某些对象到对象映射器将EF POCO对象映射到数据集,因为这些属性将被命名为相同的属性。

Here is a link on generating strongly typed datasets. 这是生成强类型数据集的链接

Here is an example how to use SqlBulkInsert. 这是一个如何使用SqlBulkInsert的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM