DapperLib / DapperLib/Dapper.Contrib

Insert with Nullable<int> Id gives "Invalid cast" error

Open
#25 12 comments 0 reactions 0 assignees View on GitHub

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.