Microsoft.EntityFrameworkCore Extension Methods Entity Framework Extensions extends your DbContext with high-performance bulk operations: BulkSaveChanges, BulkInsert, BulkUpdate, BulkDelete, BulkMerge, and more. Support: SQL Server, MySQL, Oracle, PostgreSQL, SQLite, and more! Example: https://dotnetfiddle.net/RQ0kFz Benchmark: https://dotnetfiddle.net/vZQnZt **IMPORTANT** - For EF Core 10.x, use the latest EF Extensions v10.x version - For EF Core 9.x, use the latest EF Extensions v9.x version - For EF Core 8.x, use the latest EF Extensions v8.x version - For EF Core 7.x, use the latest EF Extensions v7.x version - For EF Core 6.x, use the latest EF Extensions v6.x version - For EF Core 5.x, use the latest EF Extensions v5.x version - For EF Core 3.x, use the latest EF Extensions v3.x version - For EF Core 2.x, use the latest EF Extensions v2.x version Include free and prime features.
$ dotnet add package Z.EntityFramework.Extensions.EFCoreEF Extensions is a library that adds high performances bulk extension methods such as BulkInsert, BulkUpdate, BulkDelete, BulkMerge, and more.
You don't believe us? Just try our live benchmark on .NET Fiddle: https://dotnetfiddle.net/vZQnZt
| Action | 1000 Entities | 2000 Entities | 5000 Entities |
|---|---|---|---|
| SaveChanges (EF Core 8) | 300 ms | 600 ms | 1400 ms |
| BulkSaveChanges | 125 ms | 225 ms | 450 ms |
| BulkInsert | 35 ms | 45 ms | 75 ms |
Using our library will not only allow you to perform bulk operations 5 to 20 times faster but also to consume only 20% of the memory that SaveChanges requires. See Memory & Performance Improvements
The best part of Entity Framework Extensions is how it is integrated so easily with EF Core by simply adding the library to your project. Allowing you to use the power of bulk operations with minimal code changes. It's designed with flexibility in mind, providing you with control over the behavior of operations to suit your specific needs.
Whether you're building a small application or an enterprise-grade system, EF Extensions can help make your data operations faster, more scalable, and more efficient.
With the trust and endorsement of over 5000 customers worldwide, Entity Framework Extensions continues to lead the way in efficient data management.
Resources:
Adding Entity Framework Extensions in your projects can unlock multiple advantages:
Entity Framework Extensions actively supports all versions of EF Core. To determine the correct version of our library for your needs, you simply match the major version of EF Core with the corresponding major version of EF Extensions.
Here's a quick guide:
Always ensure you're using the latest version of EF Extensions that corresponds to your EF Core version for the best performance.
Entity Framework Extensions offers a robust set of bulk and batch operations that can significantly optimize your performance when saving or querying data:
ExecuteUpdate in EF Core 7+.ExecuteDelete in EF Core 7+.Please refer to the documentation page for a more detailed explanation and examples of each operation.
Additional features (all free) are released under the https://entityframework-plus.net/ library.
Which Include:
One of the major advantages of Entity Framework Extensions is the simplicity and ease of use, especially for those already familiar with EF Core. For instance, if you need to insert thousands of customers into your database, a single line of code is all you need:
context.BulkInsert(customers);
This line use the BulkInsert method that extends your DbContext, offering you the power of bulk operations.
Additionally, if your task involves inserting only the customers that don't already exist in your database, you can achieve this with the InsertIfNotExists option, such as:
context.BulkInsert(customers, options => { options.InsertIfNotExists = true; });
This way, the operation becomes not only efficient but also intelligent, helping you maintain the integrity of your data.
To try various implementations with the library, visit our collection of examples that you can try online:
These examples are designed to provide practical understanding of Entity Framework Extensions, showcasing its powerful features and flexible options in different scenarios.
The EF Core Bulk Insert method is the optimal solution when you need to insert thousands of entities into your database efficiently.
Here are some of the common options available:
Here is an example demonstrating the use of AutoMapOutputDirection and InsertIfNotExists options with the BulkInsert method:
context.BulkInsert(customers, options => {
options.AutoMapOutputDirection = false;
options.InsertIfNotExists = true;
});
These options offer extensive control over your bulk insert operations, making Entity Framework Extensions a powerful tool for managing large-scale data operations.
When dealing with large-scale updates, the EF Core Bulk Update method of Entity Framework Extensions offers various configuration options to optimize and tailor your operations:
The following examples illustrate the difference between ColumnInputExpression and ColumnInputNames when setting column names to update:
context.BulkUpdate(customers, options => {
options.ColumnInputExpression = x => new { x.FirstName, x.LastName };
});
context.BulkUpdate(customers, options => {
options.ColumnInputNames = new List<string>() { "FirstName", "LastName" };
});
These examples indicate the flexibility in specifying column to update, either by expression or directly by column names, showcasing the versatility of Entity Framework Extensions in catering to different operational requirements.
The EF Core Bulk Delete method offers several options for configuration, allowing you to fine-tune your delete operations to suit different scenarios:
In the following example, we demonstrate how to use the DeleteMatchedAndFormula option to delete a list of customers who were created more than two years ago:
context.BulkDelete(customers, options => {
options.DeleteMatchedAndFormula = "DestinationTable.CreatedDate < DATEADD(YEAR, -2, GETDATE())";
});
By using these options, Entity Framework Extensions allows you to implement complex conditions and custom logic within your bulk delete operations, granting you the ability to handle large-scale data deletions in a highly controlled and efficient manner.
The EF Core BulkMerge method performs an "upsert" operation: it updates existing entities and inserts non-existing ones.
Here are the common options available for configuration:
In the following example, we demonstrate the use of a custom key to update a list of customers using the ColumnPrimaryKeyExpression option. We also selectively ignore audit columns during the insert or update phase using the IgnoreOnMergeInsertExpression and IgnoreOnMergeUpdateExpression options respectively:
context.BulkMerge(customers, options => {
options.ColumnPrimaryKeyExpression = x => new { x.Code };
options.IgnoreOnMergeInsertExpression = x => new { x.UpdatedDate, x.UpdatedBy };
options.IgnoreOnMergeUpdateExpression = x => new { x.CreatedDate, x.CreatedBy };
});
By effectively utilizing these options, the Entity Framework Extensions allow for robust and flexible upsert operations, enabling complex data manipulation with relative ease and efficiency.
The EF Core WhereBulkContains method is an enhanced version of the Where LINQ method, specifically tailored for cases involving thousands of entities and custom keys. It can be used with practically any type of data source such as:
Moreover, you can specify one or more columns to use for the key using either an expression or a string:
// The JOIN clause will use the default entity key (CustomerID) if none is specified
var customers = context.Customers.WhereBulkContains(deserializedCustomers);
// You can specify a custom JOIN clause with one or more properties using a Lambda Expression
var customers = context.Customers.WhereBulkContains(deserializedCustomers, x => x.Code);
// You can specify a custom JOIN clause with one or more properties using a List<string>
var customers = context.Customers.WhereBulkContains(deserializedCustomers, new List<string> { "Code" });
// You can specify a custom JOIN clause with one or more properties using a params string[]
var customers = context.Customers.WhereBulkContains(deserializedCustomers, "Code");
By using that method, Entity Framework Extensions enable you to perform complex queries involving large amounts of data, while maintaining high performance and flexible querying capabilities.
For a comprehensive list of enhancements, fixes, and improvements in each version of the Entity Framework Extensions, refer to the official Release Notes on the GitHub repository.
This documentation provides important details about each version, including new features, resolved issues, and breaking changes (if any). Please ensure you review the release notes before upgrading to newer versions to make the best use of the features and avoid any potential issues.
Entity Framework Extensions operates under a paid licensing model. To purchase a license, please visit the official Pricing Page on the Entity Framework Extensions website.