Dapper Orm delete exception

c# dapper

Question

I'am triying to delete with dapper orm. But im getting this exception:

When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id

My code is shown below:

public void DeleteRole(int ID)
    {
        using (var conn = new SqlConnection(connectionString))
        {
            conn.Open();
            conn.Query("DELETE FROM [Role] WHERE ID=@ID", new {ID=ID });
        }
    }

Any idea?

Accepted Answer

The issue here is that you're using Query instead of Execute. The Query method is trying to find a column named Id to build the result set and can't because it's not a query.


Popular Answer

Either specify return type for query (integer)

int rowsCount = conn.Query<int>("DELETE FROM [Role] WHERE ID = @ID", new { ID });

Or use Execute method, as Michael pointed

NOTE: You don't need to open connection manually - Dapper will open it for you.

BTW generic query will work for your original question:

int id = conn.Query<int>(@"INSERT [Role] (Name, CreatedDate,UpdatedDate) 
                           VALUES (@Name, @CreatedDate,@UpdatedDate) 
                           SELECT CAST(scope_identity() as INT)", model).First();



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