Entity Framework Extensions Bulk Options

Entity Framework Extensions have been created to be fast but also to be extensible, with hundreds of options available. Understanding available options is essential to make your Bulk Extensions more flexible, but also to know how to pass them:

Fluent API Options

Passing options through a fluent API is recommended in Entity Framework Extensions. This technique is simple and matches the way you write your LINQ syntax.

You can pass 1 option by directly setting it or use braces to pass multiple options, such as

// only work with 1 option
context.BulkInsert(invoices, options => options.IncludeGraph = true);

// work with multiple options
context.BulkInsert(invoices, options => { options.IncludeGraph = true });
context.BulkInsert(invoices, options => { 
    options.IncludeGraph = true;
    options.BatchTimeout = 180;
});

Try it

BulkOperationOptions Variable

Starting from v8.101.0, you can now pass options by creating a new Z.BulkOperations.BulkOperationOptions instance.

You can either choose to:

  • From a DbSet
  • Creating an instance with a generic type
  • Creating an instance without a generic type
// CREATE bulkOptions instance
var bulkOptions = context.Invoices.CreateBulkOptions();
var bulkOptions_Generic = new Z.BulkOperations.BulkOperationOptions<Invoice>();
var bulkOptions_NonGeneric = new Z.BulkOperations.BulkOperationOptions();

// SET Options
bulkOptions.IncludeGraph = true;
bulkOptions.BatchTimeout = 180;

// CALL a bulk method
context.BulkInsert(invoices, bulkOptions);

Try it

Conclusion

Whether you pass options with a fluent API or through a BulkOperationOptions variable is a personal choice. Both techniques are now supported, and sometimes, a technique might be more appropriate than another one, but by default, we recommend sticking with only one in your code.


Last updated: 2023-11-16
Author:


Contents