Closed andrewjsaid closed 2 years ago
If I understand the code correctly, I can't see any way for this library to handle this 🤔 Convert.ChangeType()
requires that the value
supports the target type, which in this case, would mean that ulong
would have to support PersonId
.
I feel like the fix is for the Dapper.Contrib's InsertAsync
function to use the registered SqlMapper.TypeHandler<>
to convert the value? Maybe it's worth opening an issue in that project instead?
I think you're right; this is an issue for Dapper.Contrib.
I'll close this for now, as I don't think there's anything I can do, but feel free to re-open it if anything changes 🙂
Summary
I'm having trouble integrating this library into my project which uses Dapper.Contrib to insert entities to a mysql database.
Dapper.Contrib throws
System.InvalidCastException: 'Invalid cast from 'System.UInt64' to 'StronglyTypedIdTest.PersonId'.
when callingInsertAsync
. This is because of a combination of mysql'sLAST_INSERT_ID()
returning a ulong and Dapper.Contrib's InsertAsync function for mysql using LAST_INSERT_ID() and using the methodConvert.ChangeType(id, idp.PropertyType)
to set the Id property on the object.Code to Reproduce
Solution
Unfortunately the way that the conversion happens with
Convert.ChangeType
makes it difficult to find a real solution. I wanted to log this issue to see if anybody else can figure one out.Thanks for the great library and associated articles.