简体   繁体   English

如何使用实体框架对与外键相关的多个表执行批量插入

[英]How to perform bulk insert into multilple tables which are related with foreign keys using entity framework

I have to perform bulk insert(around 50-100 rows) into multiple tables( around 30 tables), among them some tables are interrelated with foreign keys. 我必须对多个表(大约30个表)执行批量插入(大约50-100行),其中一些表与外键相关。 I want to do this by using entity framework(EF). 我想通过使用实体框架(EF)来做到这一点。 But i want this happened with minimum db hit instead to of calling context.SaveChanges() for each table. 但是我希望这种情况发生在数据库命中最少的情况下,而不是为每个表调用context.SaveChanges()。 Is there any way in EF to perform this? EF中有什么方法可以执行此操作? If it so please let me know. 如果是这样,请告诉我。 Thanks in advance! 提前致谢!

Entity Framework doesn't provide a Bulk feature. Entity Framework不提供Bulk功能。

For every row you want to save, a database round-trip is required. 对于要保存的每一行,都需要数据库往返。


Disclaimer : I'm the owner of Entity Framework Extensions 免责声明 :我是Entity Framework Extensions的所有者

This library is not free but allows you to perform a BulkSaveChanges which work like SaveChanges but faster: 该库不是免费的,但允许您执行BulkSaveChanges ,其工作方式类似于SaveChanges但速度更快:

  • Bulk SaveChanges 批量保存更改
  • Bulk Insert 批量插入
  • Bulk Delete 批量删除
  • Bulk Update 批量更新
  • Bulk Merge 批量合并

Example

// Easy to use
context.BulkSaveChanges();

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

// Perform Bulk Operations
context.BulkDelete(customers);
context.BulkInsert(customers);
context.BulkUpdate(customers);

// Customize Bulk Operations
context.BulkInsert(customers, options => {
   options => options.IncludeGraph = true;
});
context.BulkMerge(customers, options => {
   options.ColumnPrimaryKeyExpression = 
        customer => customer.Code;
});

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

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