borisdj / EFCore.BulkExtensions

Entity Framework EF Core efcore Bulk Batch Extensions with BulkCopy in .Net for Insert Update Delete Read (CRUD), Truncate and SaveChanges operations on SQL Server, PostgreSQL, MySQL, SQLite
https://codis.tech/efcorebulk
Other
3.68k stars 590 forks source link

Sequence contains no matching element #631

Closed willisga closed 3 years ago

willisga commented 3 years ago

Hello, thank you very much for solving the issue 629

The following exception occurs when the destination table (Merge) is not associated with an entity.

Exception Line : https://github.com/borisdj/EFCore.BulkExtensions/blob/e6cea2398b64a3d81fd38860e79857f75a16913d/EFCore.BulkExtensions/TableInfo.cs#L172

Message: Sequence contains no matching element StackTrace:

   at System.Linq.ThrowHelper.ThrowNoMatchException()
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
   at EFCore.BulkExtensions.TableInfo.<>c__DisplayClass159_0`1.<LoadData>b__3(String b)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](List`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at EFCore.BulkExtensions.TableInfo.LoadData[T](DbContext context, Type type, IList`1 entities, Boolean loadOnlyPKColumn)
   at EFCore.BulkExtensions.TableInfo.CreateInstance[T](DbContext context, Type type, IList`1 entities, OperationType operationType, BulkConfig bulkConfig)
   at EFCore.BulkExtensions.DbContextBulkTransaction.Execute[T](DbContext context, Type type, IList`1 entities, OperationType operationType, BulkConfig bulkConfig, Action`1 progress)
   at EFCore.BulkExtensions.DbContextBulkExtensions.BulkInsertOrUpdate[T](DbContext context, IList`1 entities, Action`1 bulkAction, Action`1 progress, Type type)
   at Nexa.Data.EntityFramework.Core.CoreRepository`1.BulkInsertOrUpdate[TEntity](IList`1 data, Action`1 bulkAction, Action`1 progress)
   at Nexa.Microservice.Api.Outbound.EntityFramework.Repository.GenesysRepository.BulkInsertOrUpdateGenesys(List`1 data, Double percentageNotification, Action`3 progress) in D:\Privado\Fuentes\NexaPlus\Nexa.Microservice.Api.Outbound\src\Nexa.Microservice.Api.Outbound.EntityFramework\Repository\GenesysRepository.cs:line 53
   at Nexa.Microservice.Api.Outbound.EntityFramework.Repository.GenesysRepository.BulkInsertOrUpdateOperation(IList data, Double percentageNotification, Action`3 progress) in D:\Privado\Fuentes\NexaPlus\Nexa.Microservice.Api.Outbound\src\Nexa.Microservice.Api.Outbound.EntityFramework\Repository\GenesysRepository.cs:line 127
   at Nexa.Microservice.Api.Outbound.Logic.Services.Outbound.OutboundService.<>c__DisplayClass26_0`1.<BulkInsertOrUpdateAsync>b__0() in D:\Privado\Fuentes\NexaPlus\Nexa.Microservice.Api.Outbound\src\Nexa.Microservice.Api.Outbound.Logic\Services\Outbound\OutboundService.cs:line 347
   at System.Threading.Tasks.Task.InnerInvoke()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)

Here's an image to provide a bit of context

Dependence

To solve the exception create an option in the bulk config that allows me to assign the name of the target table that I will assign in the merge without affecting the current TableInfo processes.

BulkConfig.cs: https://github.com/borisdj/EFCore.BulkExtensions/blob/master/EFCore.BulkExtensions/BulkConfig.cs

        public string DestinationMergeTableName { get; set; }

Table Info.cs https://github.com/borisdj/EFCore.BulkExtensions/blob/master/EFCore.BulkExtensions/TableInfo.cs

Add Properties image

Update Method LoadData<T>(DbContext context, Type type, IList<T> entities, bool loadOnlyPKColumn) image

SqlQueryBuilder.cs File: https://github.com/borisdj/EFCore.BulkExtensions/blob/master/EFCore.BulkExtensions/SqlQueryBuilder.cs

image

Debugger: image

Query Result: image

I do not know if it is the correct way but I will be attentive to provide more information and to help in the most optimal solution defined by the repo administrator.

borisdj commented 3 years ago

BulkConfig has CustomDestinationTableName. There is Example of usage in TablePerTypeInsertTest: https://github.com/borisdj/EFCore.BulkExtensions/blob/e6cea2398b64a3d81fd38860e79857f75a16913d/EFCore.BulkExtensions.Tests/EFCoreBulkTestAtypical.cs#L703