I am using Dapper with the Dapper.SimpleCrud extension.
In my Dapper class, I have made an abstraction of the insert method, like this:
public static int Insert(object entity) {
try {
using (SqlConnection sqlConnection = new SqlConnection(connectionString)) {
sqlConnection.Open();
return sqlConnection.Insert(entity) ?? -1;
}
}
catch(Exception ex) {
// log
return -1;
}
}
This lets me call insert and pass an object of any type that is in the db. The code that currently calls this method looks like this:
DapperORM.Insert(new Menu {
RestaurantID = RestaurantID,
Name = Name});
This throws an error: {"Incorrect syntax near ')'."}
Ok, so now I think there is something wierd with the data I pass in or something. But no. When I change my own Insert method to take a Menu-object instead of a general object, it works.
The Dapper.SimpleCrud Insert overload method obviously can't figure out which object it is. Do you know how to fix it?
Have you had a look at the generated SQL? In stack trace may be? I guess it must be missing name of database table. Yes; I guess. Because I never used SimpleCRUD.
How do SimpleCRUD know that the
objectyou send in is "of type that is in the db"?I think
objecttype parameter is the problem. To accept "an object of any type that is in the db" you should consider converting your method to use generic type instead ofobject.This confirms my earlier diagnosis.
Convert your method to something like below:
or
or similar as per your other code structure.