From 320a3829adab66bb4bed29ed4b97caa5b490c95b Mon Sep 17 00:00:00 2001 From: Enis Necipoglu Date: Mon, 28 Nov 2022 14:20:10 +0300 Subject: [PATCH 01/10] Initial state of Bulk Operations article --- .../2022-11-28-ef-bulk-operations/POST.md | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md diff --git a/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md b/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md new file mode 100644 index 0000000000..3f679abf98 --- /dev/null +++ b/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md @@ -0,0 +1,57 @@ +# Entity Framework Core Bulk Operations +Entity Framework tracks all the entity changes and applies those changes to database one by one when `SaveChanges()` method called. There was no way to execute bulk operations in Entity Framework Core without a dependency. + +As you know [Entity Framework Extensions](https://entityframework-extensions.net/bulk-savechanges) library were doing it but it was not free. + +There was no other solution until now. [Bulk Operations](https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/whatsnew#executeupdate-and-executedelete-bulk-updates) are now available in Entity Framework Core with .NET 7. + +With .NET 7, there are two new methods such as `ExecuteUpdate` and `ExecuteDelete` available to execute bulk operations. It's a similar usage with Entity Framework Core Extensions library if you're familiar with it. + +You can visit the microsoft example [here](https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/whatsnew#executeupdate-and-executedelete-bulk-updates) about how to use it. + + +## Bulk Operations in ABP Framework + +ABP Framework supports Entity Framework Core as its ORM. So, you can use these new methods in your repository classes in your ABP applications. But there is another feature that ABP Framework provides: `IEfCoreBulkOperationProvider`. You can visit the [documentation](https://docs.abp.io/en/abp/latest/Entity-Framework-Core#customize-bulk-operations) to learn more about it. You always have full control of execution of bulk operations in your ABP applications. When you call `InsertMany`, `DeleteMany` or `UpdateMany` methods of repository, that provider will be invoked and you can customize all the queries from one point. Mostly you don't need to implement & customize that interface, but it's good to know that you can do it if you need. + +## Usage in Repositories + +Most of real world case scenarios, you need custom queries to execute bulk operations. You can't manage all the cases with overriding `IEfCoreBulkOperationProvider` even you can't identify entities in this provider because it's not aware of the properties of entities. Best way to use this new feature is inside the repositories. + +Let me show an example below: + +```csharp +public class BookEntityFrameworkCoreRepository : EfCoreRepository, IBookRepository +{ + public BookEntityFrameworkCoreRepository(IDbContextProvider dbContextProvider) : base(dbContextProvider) + { + } + + public async Task UpdateListingAsync() + { + var dbSet = await GetDbSetAsync(); + + dbSet + .Where(x => x.IsListed && x.PublishedOn.Year <= 2022) + .ExecuteUpdate(s => s.SetProperty(x => x.IsListed, x => false)); + } + + public async Task DeleteOldBooksAsync() + { + var dbSet = await GetDbSetAsync(); + + dbSet.Where(x => x.PublishedOn.Year <= 2000) + .ExecuteDelete(); + } + + public async Task ImportAsync(List books) + { + var dbSet = await GetDbSetAsync(); + + // No need an action for bulk inserting. + await base.InsertManyAsync(books); + } +} +``` + +> There is no need to take an action for bulk inserting. You can use `InsertManyAsync` method of repository instead of creating a new method for it if you don't have a custom logic. It'll use new bulk inserting feature autmatically since it's available in EF Core 7.0. \ No newline at end of file From a359b6a16d83b2e384fe653aeef90f9decf6823b Mon Sep 17 00:00:00 2001 From: Enis Necipoglu Date: Mon, 28 Nov 2022 14:27:46 +0300 Subject: [PATCH 02/10] Spellcheck fix --- .../en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md b/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md index 3f679abf98..d439edd070 100644 --- a/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md +++ b/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md @@ -54,4 +54,4 @@ public class BookEntityFrameworkCoreRepository : EfCoreRepository There is no need to take an action for bulk inserting. You can use `InsertManyAsync` method of repository instead of creating a new method for it if you don't have a custom logic. It'll use new bulk inserting feature autmatically since it's available in EF Core 7.0. \ No newline at end of file +> There is no need to take an action for bulk inserting. You can use `InsertManyAsync` method of repository instead of creating a new method for it if you don't have a custom logic. It'll use new bulk inserting feature automatically since it's available in EF Core 7.0. \ No newline at end of file From 84b7b34211193a056a7fd43eaa3f7a71d26a319f Mon Sep 17 00:00:00 2001 From: Hamza Albreem <94292623+braim23@users.noreply.github.com> Date: Mon, 28 Nov 2022 14:45:51 +0300 Subject: [PATCH 03/10] quick fix --- .../2022-11-28-ef-bulk-operations/POST.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md b/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md index d439edd070..1aa356bf3e 100644 --- a/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md +++ b/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md @@ -1,22 +1,22 @@ # Entity Framework Core Bulk Operations -Entity Framework tracks all the entity changes and applies those changes to database one by one when `SaveChanges()` method called. There was no way to execute bulk operations in Entity Framework Core without a dependency. +Entity Framework tracks all the entity changes and applies those changes to the database one by one when the `SaveChanges()` method is called. There was no way to execute bulk operations in Entity Framework Core without a dependency. -As you know [Entity Framework Extensions](https://entityframework-extensions.net/bulk-savechanges) library were doing it but it was not free. +As you know the [Entity Framework Extensions](https://entityframework-extensions.net/bulk-savechanges) library was doing it but it was not free. There was no other solution until now. [Bulk Operations](https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/whatsnew#executeupdate-and-executedelete-bulk-updates) are now available in Entity Framework Core with .NET 7. -With .NET 7, there are two new methods such as `ExecuteUpdate` and `ExecuteDelete` available to execute bulk operations. It's a similar usage with Entity Framework Core Extensions library if you're familiar with it. +With .NET 7, there are two new methods such as `ExecuteUpdate` and `ExecuteDelete` available to execute bulk operations. It's a similar usage with the Entity Framework Core Extensions library if you're familiar with it. You can visit the microsoft example [here](https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/whatsnew#executeupdate-and-executedelete-bulk-updates) about how to use it. ## Bulk Operations in ABP Framework -ABP Framework supports Entity Framework Core as its ORM. So, you can use these new methods in your repository classes in your ABP applications. But there is another feature that ABP Framework provides: `IEfCoreBulkOperationProvider`. You can visit the [documentation](https://docs.abp.io/en/abp/latest/Entity-Framework-Core#customize-bulk-operations) to learn more about it. You always have full control of execution of bulk operations in your ABP applications. When you call `InsertMany`, `DeleteMany` or `UpdateMany` methods of repository, that provider will be invoked and you can customize all the queries from one point. Mostly you don't need to implement & customize that interface, but it's good to know that you can do it if you need. +ABP Framework supports Entity Framework Core as its ORM. So, you can use these new methods in your repository classes in your ABP applications. But there is another feature that the ABP Framework provides: `IEfCoreBulkOperationProvider`. You can visit the [documentation](https://docs.abp.io/en/abp/latest/Entity-Framework-Core#customize-bulk-operations) to learn more about it. You always have full control of the execution of bulk operations in your ABP applications. When you call the `InsertMany`, `DeleteMany` or `UpdateMany` methods of the repository, that provider will be invoked and you can customize all the queries from one point. You don't mostly need to implement & customize that interface, but it's good to know that you can do it if you need. ## Usage in Repositories -Most of real world case scenarios, you need custom queries to execute bulk operations. You can't manage all the cases with overriding `IEfCoreBulkOperationProvider` even you can't identify entities in this provider because it's not aware of the properties of entities. Best way to use this new feature is inside the repositories. +In most of the real world case scenarios, you need custom queries to execute bulk operations. You can't manage all the cases by overriding `IEfCoreBulkOperationProvider` and you can't even identify the entities in this provider because it's not aware of the properties of the entities. The best way to use this new feature is inside the repositories. Let me show an example below: @@ -48,10 +48,10 @@ public class BookEntityFrameworkCoreRepository : EfCoreRepository There is no need to take an action for bulk inserting. You can use `InsertManyAsync` method of repository instead of creating a new method for it if you don't have a custom logic. It'll use new bulk inserting feature automatically since it's available in EF Core 7.0. \ No newline at end of file +> There is no need to take an action for bulk inserting. You can use the `InsertManyAsync` method of the repository instead of creating a new method for it if you don't have custom logic. It'll use a new bulk inserting feature automatically since it's available in EF Core 7.0. From dcf42700b231d5f4e627b9821794b5c9d14743d5 Mon Sep 17 00:00:00 2001 From: Enis Necipoglu Date: Mon, 28 Nov 2022 15:30:11 +0300 Subject: [PATCH 04/10] Update POST.md --- .../en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md b/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md index 1aa356bf3e..84666a59da 100644 --- a/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md +++ b/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md @@ -55,3 +55,5 @@ public class BookEntityFrameworkCoreRepository : EfCoreRepository There is no need to take an action for bulk inserting. You can use the `InsertManyAsync` method of the repository instead of creating a new method for it if you don't have custom logic. It'll use a new bulk inserting feature automatically since it's available in EF Core 7.0. + +> `ExecuteDeleteAsync` breaks the soft delete and auditing since deleted entities are removed from the change tracker. ABP can't handle it. So, you have to be careful while using it. \ No newline at end of file From 95bd723edf2b0e3f58bb2a3a1e7dcbe8cb275b17 Mon Sep 17 00:00:00 2001 From: Enis Necipoglu Date: Mon, 28 Nov 2022 15:54:41 +0300 Subject: [PATCH 05/10] Update title of article --- .../en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md b/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md index 84666a59da..fe4f677c4a 100644 --- a/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md +++ b/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md @@ -1,4 +1,4 @@ -# Entity Framework Core Bulk Operations +# Bulk Operations with Entity Framework Core 7.0 Entity Framework tracks all the entity changes and applies those changes to the database one by one when the `SaveChanges()` method is called. There was no way to execute bulk operations in Entity Framework Core without a dependency. As you know the [Entity Framework Extensions](https://entityframework-extensions.net/bulk-savechanges) library was doing it but it was not free. From b902b182c975d7002669e7d1cef4a6e1e5ed9767 Mon Sep 17 00:00:00 2001 From: Enis Necipoglu Date: Mon, 28 Nov 2022 17:41:11 +0300 Subject: [PATCH 06/10] Update docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Halil İbrahim Kalkan --- .../en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md b/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md index fe4f677c4a..f138b7ef91 100644 --- a/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md +++ b/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md @@ -56,4 +56,4 @@ public class BookEntityFrameworkCoreRepository : EfCoreRepository There is no need to take an action for bulk inserting. You can use the `InsertManyAsync` method of the repository instead of creating a new method for it if you don't have custom logic. It'll use a new bulk inserting feature automatically since it's available in EF Core 7.0. -> `ExecuteDeleteAsync` breaks the soft delete and auditing since deleted entities are removed from the change tracker. ABP can't handle it. So, you have to be careful while using it. \ No newline at end of file +> If you use `ExecuteDeleteAsync` or `ExecuteUpdateAsync`, then ABP's soft delete and auditing features can not work. Because these ABP features work with EF Core's change tracking system and these new methods doesn't work with the change tracking system. So, use them carefully. \ No newline at end of file From 1f5f615c65c33c2ff7cbad22bfc65baea407ad70 Mon Sep 17 00:00:00 2001 From: Enis Necipoglu Date: Mon, 28 Nov 2022 17:41:59 +0300 Subject: [PATCH 07/10] Update POST.md --- .../2022-11-28-ef-bulk-operations/POST.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md b/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md index f138b7ef91..27b8022fac 100644 --- a/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md +++ b/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md @@ -31,17 +31,17 @@ public class BookEntityFrameworkCoreRepository : EfCoreRepository x.IsListed && x.PublishedOn.Year <= 2022) - .ExecuteUpdate(s => s.SetProperty(x => x.IsListed, x => false)); + .ExecuteUpdateAsync(s => s.SetProperty(x => x.IsListed, x => false)); } public async Task DeleteOldBooksAsync() { var dbSet = await GetDbSetAsync(); - dbSet.Where(x => x.PublishedOn.Year <= 2000) - .ExecuteDelete(); + await dbSet.Where(x => x.PublishedOn.Year <= 2000) + .ExecuteDeleteAsync(); } public async Task ImportAsync(List books) From 2db6d5516216ee1f4bd56a5b9b546383b243e323 Mon Sep 17 00:00:00 2001 From: Enis Necipoglu Date: Mon, 28 Nov 2022 17:42:37 +0300 Subject: [PATCH 08/10] Update POST.md --- .../2022-11-28-ef-bulk-operations/POST.md | 8 -------- 1 file changed, 8 deletions(-) diff --git a/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md b/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md index 27b8022fac..5b01cbcdd8 100644 --- a/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md +++ b/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md @@ -43,14 +43,6 @@ public class BookEntityFrameworkCoreRepository : EfCoreRepository x.PublishedOn.Year <= 2000) .ExecuteDeleteAsync(); } - - public async Task ImportAsync(List books) - { - var dbSet = await GetDbSetAsync(); - - // No need for an action for bulk inserting. - await base.InsertManyAsync(books); - } } ``` From e54a6e47271c45d92816a4858979fa88688c67ed Mon Sep 17 00:00:00 2001 From: Enis Necipoglu Date: Mon, 28 Nov 2022 17:45:57 +0300 Subject: [PATCH 09/10] Remove IEfCoreBulkOperationProvider reference from documentation --- .../2022-11-28-ef-bulk-operations/POST.md | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md b/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md index 5b01cbcdd8..edf88bfcaa 100644 --- a/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md +++ b/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md @@ -9,16 +9,11 @@ With .NET 7, there are two new methods such as `ExecuteUpdate` and `ExecuteDelet You can visit the microsoft example [here](https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/whatsnew#executeupdate-and-executedelete-bulk-updates) about how to use it. - -## Bulk Operations in ABP Framework - -ABP Framework supports Entity Framework Core as its ORM. So, you can use these new methods in your repository classes in your ABP applications. But there is another feature that the ABP Framework provides: `IEfCoreBulkOperationProvider`. You can visit the [documentation](https://docs.abp.io/en/abp/latest/Entity-Framework-Core#customize-bulk-operations) to learn more about it. You always have full control of the execution of bulk operations in your ABP applications. When you call the `InsertMany`, `DeleteMany` or `UpdateMany` methods of the repository, that provider will be invoked and you can customize all the queries from one point. You don't mostly need to implement & customize that interface, but it's good to know that you can do it if you need. +ABP Framework supports Entity Framework Core as its ORM. So, you can use these new methods in your repository classes in your ABP applications without any limitation. ## Usage in Repositories -In most of the real world case scenarios, you need custom queries to execute bulk operations. You can't manage all the cases by overriding `IEfCoreBulkOperationProvider` and you can't even identify the entities in this provider because it's not aware of the properties of the entities. The best way to use this new feature is inside the repositories. - -Let me show an example below: +You can use these methods in your repositories as below: ```csharp public class BookEntityFrameworkCoreRepository : EfCoreRepository, IBookRepository @@ -40,7 +35,8 @@ public class BookEntityFrameworkCoreRepository : EfCoreRepository x.PublishedOn.Year <= 2000) + await dbSet + .Where(x => x.PublishedOn.Year <= 2000) .ExecuteDeleteAsync(); } } From 777aca28e3a49012667ee4f722920db57d67982c Mon Sep 17 00:00:00 2001 From: Enis Necipoglu Date: Tue, 29 Nov 2022 23:42:45 +0300 Subject: [PATCH 10/10] Update POST.md --- .../2022-11-28-ef-bulk-operations/POST.md | 34 ++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md b/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md index edf88bfcaa..ac517506ec 100644 --- a/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md +++ b/docs/en/Community-Articles/2022-11-28-ef-bulk-operations/POST.md @@ -9,11 +9,17 @@ With .NET 7, there are two new methods such as `ExecuteUpdate` and `ExecuteDelet You can visit the microsoft example [here](https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/whatsnew#executeupdate-and-executedelete-bulk-updates) about how to use it. -ABP Framework supports Entity Framework Core as its ORM. So, you can use these new methods in your repository classes in your ABP applications without any limitation. -## Usage in Repositories +It can be easily used with the DbContext. -You can use these methods in your repositories as below: +```csharp +await context.Tags.Where(t => t.Text.Contains(".NET")).ExecuteDeleteAsync(); +``` + +## Using with ABP Framework +ABP Framework provides an abstraction over database operations and implements generic repository pattern. So, DbContext can't be accessed outside of [repositories](https://docs.abp.io/en/abp/latest/Repositories). + +You can use the `ExecuteUpdate` and `ExecuteDelete` methods inside a repository. ```csharp public class BookEntityFrameworkCoreRepository : EfCoreRepository, IBookRepository @@ -42,6 +48,24 @@ public class BookEntityFrameworkCoreRepository : EfCoreRepository There is no need to take an action for bulk inserting. You can use the `InsertManyAsync` method of the repository instead of creating a new method for it if you don't have custom logic. It'll use a new bulk inserting feature automatically since it's available in EF Core 7.0. +There is no need to take an action for bulk inserting. You can use the `InsertManyAsync` method of the repository instead of creating a new method for it if you don't have custom logic. It'll use a new bulk inserting feature automatically since it's available in EF Core 7.0. + +```csharp +public class MyDomainService : DomainService +{ + protected IRepository BookRepository { get; } + + public MyDomainService(IRepository bookRepository) + { + BookRepository = bookRepository; + } + + public async Task CreateBooksAsync(List books) + { + // It'll use bulk inserting automatically. + await BookRepository.InsertManyAsync(books); + } +} +``` -> If you use `ExecuteDeleteAsync` or `ExecuteUpdateAsync`, then ABP's soft delete and auditing features can not work. Because these ABP features work with EF Core's change tracking system and these new methods doesn't work with the change tracking system. So, use them carefully. \ No newline at end of file +> If you use `ExecuteDeleteAsync` or `ExecuteUpdateAsync`, then ABP's soft delete and auditing features can not work. Because these ABP features work with EF Core's change tracking system and these new methods doesn't work with the change tracking system. So, use them carefully.