C# 实体框架中的批量插入

C# 实体框架中的批量插入,c#,sql-server,entity-framework,C#,Sql Server,Entity Framework,我使用批量插入插入大量记录,如20K,当我只插入一个实体时,它将工作正常。但是,当我习惯于插入多个实体(如一对多)时,它将只插入父实体,而不插入子实体 我的实体和代码 Customer.cs public class Customer { public Customer() { this.AccountCustomers = new HashSet<AccountCustomer>(); }

我使用批量插入插入大量记录,如20K,当我只插入一个实体时,它将工作正常。但是,当我习惯于插入多个实体(如一对多)时,它将只插入父实体,而不插入子实体

我的实体和代码

Customer.cs
    public class Customer
     {
        public Customer()
        {
           this.AccountCustomers = new HashSet<AccountCustomer>();
        }
        public int CustomerId{get;set;}
        public int CustomerName{get;set;}
        public virtual ICollection<AccountCustomer> AccountCustomers { get; set; }
     }

 AccountCustomer.cs
     public partial class AccountCustomer
      {
       public int CustomerId { get; set; }        
       public string CustomData { get; set; }
       public System.DateTime CreatedDate { get; set; }       

       public virtual Customer Customer { get; set; }
      }

My code:
  List<Customer> customerList = CreateCustomer();
  for (int index = 0; index < 20000;index++ )
        {
            Customer customer = new Customer();
            customer.CustomerName= "Parthi";
            AccountCustomer accountCustomer = new AccountCustomer();
            accountCustomer.CustomdData= "customdata";
            accountCustomer.CreatedDate = DateTime.UtcNow;
            customer.AccountCustomers.Add(accountCustomer);
            customerList.Add(customer);
        }

      private static void AddCustomer(List<Customer> customerList)
      {
        using (var ctx =new Directdialogs())
        {
            using (var transactionScope = new TransactionScope())
            {
                try
                {
                    ctx.BulkInsert(customerList);
                    ctx.SaveChanges();
                    transactionScope.Complete();
                }
                catch(Exception ex)
                {
                    transactionScope.Dispose();
                }
            }
        }
    }
Customer.cs
公共类客户
{
公众客户()
{
this.AccountCustomers=new HashSet();
}
public int CustomerId{get;set;}
公共int客户名称{get;set;}
公共虚拟ICollection AccountCustomers{get;set;}
}
AccountCustomer.cs
公共部分类帐户客户
{
public int CustomerId{get;set;}
公共字符串CustomData{get;set;}
public System.DateTime CreatedDate{get;set;}
公共虚拟客户客户{get;set;}
}
我的代码:
List customerList=CreateCustomer();
对于(int-index=0;index<20000;index++)
{
客户=新客户();
customer.CustomerName=“Parthi”;
AccountCustomer=新AccountCustomer();
accountCustomer.CustomdData=“customdata”;
accountCustomer.CreatedDate=DateTime.UtcNow;
customer.AccountCustomers.Add(accountCustomer);
customerList.Add(客户);
}
私有静态void AddCustomer(列表customerList)
{
使用(var ctx=new Directdialogs())
{
使用(var transactionScope=new transactionScope())
{
尝试
{
ctx.BulkInsert(客户列表);
ctx.SaveChanges();
transactionScope.Complete();
}
捕获(例外情况除外)
{
transactionScope.Dispose();
}
}
}
}
这是我的代码,但它将只插入客户实体数据而不插入帐户客户数据,批量插入不支持插入多个实体任何人都知道的帮助我

提前感谢。

您正在使用的项目(
BulkInsert
来自此第三方项目,而不是EF6)一次只支持批量插入一个表

如果您希望项目能够一次插入多个表,以便能够处理子实体,则需要修改代码以自己完成(如果您自己编写代码,请共享代码并将其贡献回项目)


编辑:然而,这可能比乍一看要困难得多。在大容量插入操作期间,您将无法知道哪些列的标识值(无论是
int
标识还是
uniqueidentifier
)设置为可靠,因此设置外键关系可能非常困难。在插入之前,您可能需要预先设置任何标识值。

BulkInsert
是一种扩展方法。什么是
BulkInsert
?EFIt中没有什么东西在Entity framework 6 friend中。您确定没有安装吗?在我在nuget Package中安装了此“Install Package EntityFramework.BulkInsert-ef6”之后,您确定它不是DbContext的一部分。