Dapper.SimpleCRUD Insert issue without identity

c# dapper dapper-simplecrud

Question

I have table User

CREATE TABLE [User](
    [Id] [int] primary key NOT NULL,
    [Username] [nvarchar](50) NULL,
    [EmailID] [nvarchar](50) NULL)

User class

public class User
{
    public int Id { get; set; }
    public string Username { get; set; }
    public string EmailID { get; set; }
}

code

var usr=new User();
usr.Id=1;
usr.Username="jhon";
usr.EmailID="j@gmail.com";
_dbConnection.Insert<User>(usr);

The above code throwing null exception.Table with identity working fine.

Stack Trace:

at Dapper.SimpleCRUD.Insert[TKey](IDbConnection connection, Object entityToInsert, IDbTransaction transaction, Nullable`1 commandTimeout)

Error Message:

An exception of type 'System.Exception' occurred in Dapper.SimpleCRUD.dll but was not handled in user code Additional information: Invalid return type

Popular Answer

I ran into the same issue today. It's because you're specifying the type to be inserted instead of specifying the return type which would be int. With that said however you don't even need to specify the type.

Your code can be simplified to:

var usr=new User();
usr.Id=1;
usr.Username="jhon";
usr.EmailID="j@gmail.com";
_dbConnection.Insert(usr);


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why