DapperLib / DapperLib/Dapper.Contrib

Dapper.Contrib "Insert" throws "Cannot convert null to 'int' because it is a non-nullable value type" when trying to call insert extension method while not using identity column at database

Open
#48 10 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

this is the issue. create a table in the database without identity column. create its poco class and try to call insert method.
I would like to set id column from my application. that is why i am not using identity column.

i fould the reason when i debug the code. After I call Insert extension method, it tries to return @@Identity value as integer. but because of id is not identity, it returns null. so i get "Cannot convert null to 'int' because it is a non-nullable value type" exception message.

CREATE TABLE User(
    [Id] [int] Primary key NOT NULL ,
    [Name] nvarchar(50) NOT NULL
)
public class User
{
 public Int32 Id {get;set;}
 public String Name {get;set;}
}

using Dapper.Contrib.Extensions;

var connStr = "bla bla";
var connection = new SqlConnection(connStr);
connection.Open();
connection.Insert(new User { Id = 1, Name="Tugbay" });
connection.Close();

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 at the Dapper.Contrib Insert extension method and reproduce the issue with the SQL table and C# User class shown in the report. Trace how the insert handles @@Identity when the Id is supplied by the application; done means inserting a row without an identity column no longer fails while identity-column inserts remain supported.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.