Invalid cast from 'System.String' to 'System.Guid'
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 18.4k
- Forks
- 3.7k
- Avg merge
- 5h 8m
- Merged PRs (30d)
- 1
Description
My Id is defined in tSQL as
id NVARCHAR (128) DEFAULT (NEWID()) NOT NULL
and in C# as
public Guid Id { get; set; }
Dapper generates an error Invalid cast from 'System.String' to 'System.Guid'. One solution (from here) is to add a private IdString thus
private string IdString { get; set; }
public Guid Id
{
get
{
return new Guid(IdString);
}
set
{
IdString = value.ToString();
}
}
and changing the tSQL from id to Id AS IdString.
It's a bit painful not to be able to use SELECT * because of that alias.
Using Guid as an id column seems common practice.
Should there not be a built in mechanism for casting between Guids and strings?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the reported mapping with an NVARCHAR(128) SQL id containing a GUID and a C# Guid property, then inspect Dapper's type-conversion and member-mapping path. Done should mean the existing SELECT * shape maps the string-backed id to Guid without requiring an alias or private backing property.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, sql
- Domain
- backend, databases
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100