« Entity Framework Plus » : différence entre les versions
De Banane Atomic
Aller à la navigationAller à la recherche
(Page créée avec « Category:CSharp = Links = * [https://github.com/zzzprojects/EntityFramework-Plus Entity Framework Plus] = [https://entityframework-plus.net/ef-core-batch-update Batch update] = <kode lang='csharp'> context.Items.Where(x => ids.Contains(x.Id)) .UpdateAsync(x => new Item { Name = "new name" }, x => { x.Executing = command => Console.WriteLine(command.CommandText);... ») |
|||
Ligne 4 : | Ligne 4 : | ||
= [https://entityframework-plus.net/ef-core-batch-update Batch update] = | = [https://entityframework-plus.net/ef-core-batch-update Batch update] = | ||
{{info | EF Core 7 [https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/whatsnew#basic-executeupdate-examples ExecuteUpdate] can handle this scenario}} | |||
<kode lang='csharp'> | <kode lang='csharp'> | ||
context.Items.Where(x => ids.Contains(x.Id)) | context.Items.Where(x => ids.Contains(x.Id)) |
Version du 14 août 2023 à 10:00
Links
Batch update
EF Core 7 ExecuteUpdate can handle this scenario |
context.Items.Where(x => ids.Contains(x.Id)) .UpdateAsync(x => new Item { Name = "new name" }, x => { x.Executing = command => Console.WriteLine(command.CommandText); }); |
UPDATE A SET A.[Name] = @zzz_BatchUpdate_0 FROM [Items] AS A INNER JOIN ( SELECT [i].[Id], [i].[Name] FROM [Items] AS [i] WHERE [i].[Id] IN (1, 2) ) AS B ON A.[Id] = B.[Id] |
- Updates multiples rows
- in a single database roundtrip
- without loading entities in the context
Limitations:
- Do not support Complex Type
- Do not support TPC
- Do not support TPH
- Do not support TPT
If you need to use one of this feature, you need to use the paying library Entity Framework Extensions
Batch delete
- Deletes multiples rows
- in a single database roundtrip
- without loading entities in the context
Query Future
Allow to reduce database roundtrip by batching multiple queries in the same sql command.
var futureItems = dbContext.Items.Future(); var futureCategories = dbContext.Categories.Future(); // TRIGGER all pending queries (futureItems & futureCategories) var items = await futureItems.ToListAsync(); // for immediate methods (count, first, single, contains, any), use a query Deferred var futureItemCount = await context.Items.DeferredCount().FutureValue(); var count = futureItemCount.Value; |
Query cache
var items = await context.Items.FromCacheAsync(); // The first call perform a database round trip // The following calls will take the value from the memory instead // for immediate methods (count, first, single, contains, any), use a query Deferred var count = await context.Items.DeferredCount().FromCacheAsync(); |
Audit
Allow to easily track changes, exclude/include entity or property and auto save audit entries in the database.