简体   繁体   中英

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. I want to do this by using entity framework(EF). But i want this happened with minimum db hit instead to of calling context.SaveChanges() for each table. Is there any way in EF to perform this? If it so please let me know. Thanks in advance!

Entity Framework doesn't provide a Bulk feature.

For every row you want to save, a database round-trip is required.


Disclaimer : I'm the owner of Entity Framework Extensions

This library is not free but allows you to perform a BulkSaveChanges which work like SaveChanges but faster:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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