1

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?

hellogoodnight
  • 1,989
  • 7
  • 29
  • 57

1 Answers1

1

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.

an object of any type that is in the db

How do SimpleCRUD know that the object you send in is "of type that is in the db"?

I think object type 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 of object.

When I change my own Insert method to take a Menu-object instead of a general object, it works.

This confirms my earlier diagnosis.

Convert your method to something like below:

public static int Insert<TPoco>(TPoco entity) where TPoco : class

or

public static int Insert<TPoco>(TPoco entity) where TPoco : BasePoco

or similar as per your other code structure.

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
  • You may refer [this](https://stackoverflow.com/a/45460483/5779732) example code. In that code, entire class is generic; in your case, only one method is generic. Also, that code uses different toolkit; still the concept is similar. Hope this helps you. – Amit Joshi Dec 08 '18 at 10:17