DapperLib / DapperLib/Dapper.Contrib
Insert with Nullable<int> Id gives "Invalid cast" error
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 293
- Forks
- 109
- PR merge metrics
- No merged PRs in 30d
Description
Im Dapper.Contrib 1.50.0, when the model is defined with an Id of nullable int and the SQL Server database is an INT IDENTITY(1,1), the result of a Dapper.Contrib INSERT is:
Invalid cast from 'System.Int32' to 'System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.
Reproduction:
CREATE TABLE Car
(
Id INT IDENTITY(1,1),
Model VARCHAR(50)
)
public class Car
{
public int? Id { get; private set; }
public string Model { get; private set; }
}
var id = connection.Insert(new Car { Model = "Mazda" });
This was working in Dapper.Contrib 1.43 but is broken in Dapper.Contrib 1.50.0.
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 with the Dapper.Contrib INSERT entry point and reproduce the issue using the Car table and nullable Id model shown in the report. Trace how the SQL Server identity result is assigned, then verify that inserting the Car succeeds and its generated Id is populated without an invalid-cast error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, sql
- Domain
- database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100