Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLite Invalid cast from 'System.String' to 'System.Guid' #575

Open
eliottrobson opened this issue Mar 20, 2024 · 4 comments
Open

SQLite Invalid cast from 'System.String' to 'System.Guid' #575

eliottrobson opened this issue Mar 20, 2024 · 4 comments
Assignees

Comments

@eliottrobson
Copy link

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]
@JonathanMagnan JonathanMagnan self-assigned this Mar 20, 2024
@JonathanMagnan
Copy link
Member

Hello @eliottrobson ,

Thank you for reporting. We will look at this case to try to support it.

Best Regards,

Jon

@JonathanMagnan
Copy link
Member

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

@zzzprojects zzzprojects deleted a comment from princeRenny Mar 31, 2024
@JonathanMagnan
Copy link
Member

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

@eliottrobson
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants