EFCore.BulkExtensions icon indicating copy to clipboard operation
EFCore.BulkExtensions copied to clipboard

`entitiesWithOutputIdentity` contains the ids rather than the entities

Open AntonC9018 opened this issue 1 year ago • 0 comments

I've got some code that goes something like this:

var bulkConfig = new BulkConfig
{
    SetOutputIdentity = true,
    SetOutputNonIdentityColumns = false,
    PropertiesToIncludeOnUpdate = new()
    {
        nameof(Article.Name),
        nameof(Article.ManufacturerId),
        nameof(Article.Updated),
        nameof(Article.UpdatedBy),
    },
    PropertiesToIncludeOnCompare = new()
    {
        nameof(Article.Name),
        nameof(Article.ManufacturerId),
    },
    UpdateByProperties = new()
    {
        nameof(Article.ManufacturerPartNumber),
        nameof(Article.CountryId),
        nameof(Article.SupplierCatalogId),
    },
};
await _dbContext.BulkInsertOrUpdateAsync(
    articles,
    bulkConfig,
    cancellationToken: cancellationToken);

The article entity has an int Id property as the primary key.

The library fails at that code with the following exception + stack trace:

System.NullReferenceException: Object reference not set to an instance of an object.
   at lambda_method2692(Closure, Object)
   at EFCore.BulkExtensions.FastProperty.Get(Object instance)
   at EFCore.BulkExtensions.TableInfo.UpdateEntitiesIdentity[T](TableInfo tableInfo, IEnumerable`1 entities, IEnumerable`1 entitiesWithOutputIdentity)
   at EFCore.BulkExtensions.TableInfo.LoadOutputDataAsync[T](DbContext context, Type type, IEnumerable`1 entities, TableInfo tableInfo, Boolean isAsync, CancellationToken cancellationToken)
   at EFCore.BulkExtensions.SqlAdapters.SqlServer.SqlServerAdapter.MergeAsync[T](DbContext context, Type type, IEnumerable`1 entities, TableInfo tableInfo, OperationType operationType, Action`1 progress, Boolean isAsync, CancellationToken cancellationToken)
   at EFCore.BulkExtensions.SqlAdapters.SqlServer.SqlServerAdapter.MergeAsync[T](DbContext context, Type type, IEnumerable`1 entities, TableInfo tableInfo, OperationType operationType, Action`1 progress, Boolean isAsync, CancellationToken cancellationToken)
   at EFCore.BulkExtensions.SqlAdapters.SqlServer.SqlServerAdapter.MergeAsync[T](DbContext context, Type type, IEnumerable`1 entities, TableInfo tableInfo, OperationType operationType, Action`1 progress, CancellationToken cancellationToken)
   at EFCore.BulkExtensions.SqlBulkOperation.MergeAsync[T](DbContext context, Type type, IEnumerable`1 entities, TableInfo tableInfo, OperationType operationType, Action`1 progress, CancellationToken cancellationToken)
   at EFCore.BulkExtensions.DbContextBulkTransaction.ExecuteAsync[T](DbContext context, Type type, IEnumerable`1 entities, OperationType operationType, BulkConfig bulkConfig, Action`1 progress, CancellationToken cancellationToken)
   at <my code>

I have had a hard time trying to debug this, Rider couldn't link to sources automatically, so I could only debug on the decompiled version. The error seems to be because the list entitiesWithOutputIdentity contains the ids rather than the article entities. Then it tries to get a property value from that int.

image

This is most certainly a bug in the library.

I'm on the latest version 7.1.6.

AntonC9018 avatar Oct 18 '23 20:10 AntonC9018