Search code examples
c#.netentity-framework-corecastingbulkupdate

Unable to cast object of type 'System.Linq.EmptyPartition1[Model]' to type 'System.Collections.Generic.List1[Model]' using EFCore.BulkExtensions


I'm using EFCore 7.0 and EFCoreBulkExtensions 7.1.2

When I'm trying to execute a

_context.BulkUpdate(list, bulkConfig);

It thows an <System.InvalidCastException>:

Unable to cast object of type 'System.Linq.EmptyPartition1[ModelX]' to type 'System.Collections.Generic.List1[ModelX]'

the list is not empty, it has elements of ModelX class

Any clue about this?

I already tried passing the list as List<>, IList<> and IEnumerable<> and getting the same error.


Solution

  • I changed the .BulkUpdate() method by an .UpdateRange()

    from this

    _context.BulkUpdate(list, bulkConfig);

    to this

    _context.UpdateRange(list);

    _context.SaveChanges();

    and now it's working, hopefully helps another lost developer looking for light :D