EntityFramework-Extensions
EntityFramework-Extensions copied to clipboard
SQLite Invalid cast from 'System.String' to 'System.Guid'
Description
When using value objects to wrap primitive types an error is thrown when the SQLite provider returns a string instead of a Guid.
Exception
If you are seeing an exception, include the full exceptions details (message and stack trace).
System.InvalidCastException: Invalid cast from 'System.String' to 'System.Guid'.
at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
at Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter`2.Sanitize[T](Object value)
at Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter`2.<>c__DisplayClass6_0`2.<SanitizeConverter>b__1(Object v)
at Z.EntityFramework.Extensions.PropertyZInfo.ConvertValueFromProvider(DbContext context, BulkOperation bulkOperation, Object value)
at .`1.(Int32 , String , Object )
at .SetValue(Int32 position, accessor, Object value)
at .SetValue(Int32 position, accessor, Object value)
at .( , DataSet , String , Boolean )
at .( , DbCommand )
at .Execute(List`1 actions)
at .(List`1 )
at Z.BulkOperations.BulkOperation.Execute()
at Z.BulkOperations.BulkOperation.BulkMerge()
at .BulkMerge[T](DbContext this, IEntityType entityType, IEnumerable`1 list, Action`1 options, SavingSelector savingSelector, Boolean forceSpecificTypeMapping)
at .BulkMerge[T](DbContext this, IEnumerable`1 entities, Action`1 options, Boolean isBulkSaveChanges)
at DbContextExtensions.BulkMerge[T](DbContext this, IEnumerable`1 entities, Action`1 options)
at DbContextExtensions.`1.()
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
at .[](DbContext , Func`2 , Action`1 , CancellationToken )
Fiddle or Project (Optional)
https://dotnetfiddle.net/GS6eay
Further technical details
- EF version: [EF Core v8.102.2]
- EF Extensions version: [EFE Core v8.102.2]
Hello @eliottrobson ,
Thank you for reporting. We will look at this case to try to support it.
Best Regards,
Jon
Hello @eliottrobson ,
Do you need to output the inserted Guid
to your custom CustomerID
?
If not, you can already fix it by using cfg.AutoMapOutputDirection = false;
such as:
context.Customers.BulkMerge(newCustomers, cfg =>
{
cfg.ColumnPrimaryKeyExpression = x => new { x.CustomerId };
// IgnoreOnMergeUpdateExpression ==> this one is not required (We do not update the key used)
cfg.IgnoreOnMergeUpdateExpression = x => new { x.CustomerId };
cfg.IgnoreOnMergeInsertExpression = x => new { x.CustomerId };
cfg.AutoMapOutputDirection = false;
});
Here is an example: https://dotnetfiddle.net/gGxIGu
Let me know if that solves your issue or if you really need to output this value.
Best Regards,
Jon
Hello @eliottrobson,
Since our last conversation, we haven't heard from you.
Is everything working as expected?
Let me know if you need more information.
Best regards, Jon
Apologies for not getting back to you sooner but unfortunately we do need to use the ID as part of the rest of the process. We've managed to work around this by not using BulkMerge in this specific part of the application but would be nice to get it fixed so we can take advantage of the performance at scale!