Reputation: 163
I'm using SQLProvider for making a query in F# project. In my DB I have a column which is storing GUID - 'Id' char(36) NOT NULL
. And when query is made I got an error:
Unhandled exception. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.InvalidCastException: Unable to cast object of type 'System.Guid' to type 'System.String'.
I'm using MySqlConnector as a driver.
Here is how table looks in DB
CREATE TABLE `Comics` (
`Id` char(36) NOT NULL,
`Price` double DEFAULT NULL,
`Title` char(255) DEFAULT NULL,
`Image` char(255) DEFAULT NULL,
`Description` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
Here is types which was generated based on DB
Here is a query
let ctx = sql.GetDataContext()
let ComicsTable = ctx.ComicsShop.Comics;
let getById (id: string) =
query {
for c in ComicsTable do
where (c.Id = id)
select c.Id
} |> Seq.head
And usage
let result =
getById "0e49d94e-76c4-44be-b2ea-38382b297c78"
Console.WriteLine(result)
Thanks for your help.
Upvotes: 1
Views: 1087
Reputation: 163
After quite some time of investigation the issue was with DB.
My Id
field has type as char(36)
which is converted into string. But since that column contains GUID, SQLProvider will make a query assuming that field is Guid, and it will throw an error in Runtime.
The solutions is simple enough Id
columns which is containing Guid should be typed as varchar(36)
then all types will be converted correctly.
Upvotes: 3