EFCore.BulkExtensions fails to do insert with Guid PK

4

I am using EFCore.BulkExtensions to insert an entity with a GUID / UniqueIdentifier primary key into my database, but I am receiving the following exception when I call this line:

System.InvalidOperationException HResult=0x80131509 Message=The given value of type String from the data source cannot be converted to type uniqueidentifier of the specified target column.
Source=EFCore.BulkExtensions StackTrace: at EFCore.BulkExtensions.SqlBulkOperation.Insert[T](DbContext context, IList1 entities, TableInfo tableInfo, Action1 progress) at EFCore.BulkExtensions.DbContextBulkExtensions.BulkInsert[T](DbContext context, IList1 entities, BulkConfig bulkConfig, Action1 progress)
at MyCompany.Abp.EfCore.BulkExtensions.BulkInsert[TEntity,TPrimaryKey](IRepository2 repository, IList1 entities, BulkConfig bulkConfig, Action1 progress) in D:\Projects\MySystem\Utilities\MyCompany.Abp.EfCore\BulkExtensions.BulkInsert.cs:line 21 at s.SearchServices.Infrastructure.UnscConsolidatedList.Infrastructure.s.MonitoringBatch.MonitoringBatchSearchJob.PerformScreening(Guid sourceId, IList1 Records) in D:\Projects\MySystem\Modules\s\s.SearchServices.Infrastructure.UnscConsolidatedList\Infrastructure\s\MonitoringBatch\MonitoringBatchSearchJob.cs:line 96 at s.SearchServices.Infrastructure.UnscConsolidatedList.Infrastructure.s.MonitoringBatch.MonitoringBatchSearchJob.ExecuteJob(MonitoringBatchSearhJobArgs args) in D:\Projects\MySystem\Modules\s\s.SearchServices.Infrastructure.UnscConsolidatedList\Infrastructure\s\MonitoringBatch\MonitoringBatchSearchJob.cs:line 82 at s.SearchServices.Infrastructure.UnscConsolidatedList.Core.BatchSearchJobBase`1.Execute(TBatchSearhJobArgs args) in D:\Projects\MySystem\Modules\s\s.SearchServices.Infrastructure.UnscConsolidatedList\Core\BatchSearchJobBase.cs:line 28

Inner Exception 1: InvalidCastException: Failed to convert parameter value from a String to a Guid.

Inner Exception 2: InvalidCastException: Invalid cast from 'System.String' to 'System.Guid'.

The code that throws the exception:

        public static void BulkInsert<TEntity, TPrimaryKey>(
            this IRepository<TEntity, TPrimaryKey> repository,
            IList<TEntity> entities,
            BulkConfig bulkConfig = null,
            Action<decimal> progress = null)
            where TEntity : class, IEntity<TPrimaryKey>
        {
            var db = repository.GetDbContext();
            db.BulkInsert(entities, bulkConfig, progress); // <== throws exception
        }

What am I doing wrong?

entity-framework
ef-code-first
sqlbulkcopy
ef-core-2.2
asked on Stack Overflow Jul 5, 2019 by CShark • edited Jul 8, 2019 by CShark

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0