Database table:
CREATE TABLE [dbo].[EmployeeDetails]
(
[id] [int] PRIMARY KEY IDENTITY(1,1) NOT NULL,
[Name] [varchar](max) NULL,
[Emailid] [varchar](max) NULL,
[department] [varchar](max) NULL,
[Salary] [int] NULL,
[Profile] [varchar](max) NULL,
[Description] [varchar](max) NULL,
[ReferencesId] [int] NULL
)
Model class:
public class Employee
{
#region properties
public int Id { get; set; }
public string? Name { get; set; }
public string? Emailid { get; set; }
public string? department { get; set; }
public int salary { get; set; }
public string? Profile { get; set; }
public string? Description { get; set; }
#endregion
}
ClassMappper
public class EmployeeMapper : ClassMapper<Employee>
{
public EmployeeMapper()
{
Table("EmployeeDetails");
Map(x => x.Id).Key(KeyType.Identity).Column("Id").Type(System.Data.DbType.Int32);
Map(x => x.Name).Column("Name");
Map(x => x.Emailid).Column("Emailid");
Map(x => x.department).Column("department");
Map(x => x.salary).Column("Salary");
Map(x => x.Profile).Column("Profile");
Map(x => x.Description).Column("Description");
}
}
Insert code:
public int Add(T entity)
{
int count = 0;
using (var con = _dapperContext.CreateConnection())
{
int id = con.Insert(entity);
if (id > 0)
{
count++;
}
}
return count;
}
I get this error:

System.ArgumentException: 'Object of type 'System.Int64' cannot be converted to type 'System.Int32'.'
I show this exception on the page but data is inserted in database successfully. My database datatype and model class datatypes both are the same, but I still get this "Conversion Error" on the page
I need insert one new row into the database table using Dapper Extension in ASP.NET Core Razor page
Well, at the beginning I was thinking you are doing something silly. Out of my curiosity, I get into it and got to know issue is not within your code its
Dapper-Extensionsissue. I have investigate quite a long while with their document and seems its older version and currently they are not supporting it. I have gone through this official document.Issue Reproduced:
I have have reproduced the issue which has been thrown from
Dapper-Extensionsmethod as can be seen below:Dapper Extension Method:
Exception:
Stacktrace:
Note:
While debugging and reproduce the issue, I have tried to use data type both
intin both side as well as other data type for instance,bigintbut still that conversion error taken place and I found this issue is known in regards ofDapper-Extensions. Thus, one data-type works out accordingly that isGUID. I still not sure, how theseObject value, Binder binder, CultureInfo culture, Boolean needsSpecialCastare Intervening here.Solution-Work Around:
As of now,
int,bigintalways throws'Object of type 'System.Int64' cannot be converted to type 'System.Int32'.'hence, the reason is not clear which coming fromDapperExtensions.DapperExtensions.Insert()method.Furthermore, to avoid above error, we can do as following:
POCO Model:
Note:
Other than
Guidcurrently not working, I am not sure the reason behind this. So if you would like to stick to your implementation, you could considerGuid.Database Schema:
Asp.Net Core Controller/Repository Method:
Note: Please update the code snippet based on your scenario and context.
Output:
Note: Moreover, based on the stacktrace, if someone has the exact explnation or findings, please do let me know. I wound happy to update my answer.