简体   繁体   English

实体框架中的批量插入

[英]Bulk insert in entity framework

I insert large amount of records like 20K using bulk insert,it will be working fine when I insert only one entity. 我使用批量插入插入了20K之类的大量记录,当我仅插入一个实体时,它将很好地工作。 But when I used to insert multiple entities like one to many it will be inserting only the parent entity the child entities are not inserted. 但是,当我以前插入一对多的多个实体时,它将仅插入父实体,而不会插入子实体。

My Entities and Code 我的实体和代码

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();
                }
            }
        }
    }

This is my code but it will inserted only the customer entity data not insert the account customer data, Is bulk insert not supporting to insert multiple entities anybody knows help me. 这是我的代码,但是它只会插入客户实体数据而不插入客户客户数据,批量插入不支持插入任何人都知道的帮助我。

Thanks in advance. 提前致谢。

The EntityFramework.BulkInsert project you are using ( BulkInsert comes from this 3rd party project not EF6) only supports bulk inserting to one table at a time. 您正在使用的EntityFramework.BulkInsert项目( BulkInsert来自此第三方项目而不是EF6)仅一次支持批量插入一个表。

If you want the project to be able to insert in to multiple tables at a time to be able to handle child entities you will need to modify the code to do it yourself (If you do write it yourself please share and contribute the code back to the project). 如果您希望项目能够一次插入多个表中以处理子实体,则需要修改代码以自己完成(如果您自己编写,请共享并将代码贡献给该项目)。

EDIT: However, this may be a lot harder than you think at first glance. 编辑:但是,这可能比乍一看要难得多。 You will have no way to know what columns with Identity values (be it a int identity or a uniqueidentifier ) where set to during the bulk insert operation reliably, so setting up Foreign Key relationships may be very hard to do. 您将无法可靠地知道在批量插入操作期间将哪些Identity列(具有int身份或uniqueidentifier )可靠地设置在哪里,因此设置外键关系可能非常困难。 You may need to pre-set any identity values before you insert. 插入之前,您可能需要预先设置所有标识值。

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

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