ftdeveloper
ftdeveloper

Reputation: 1093

Dapper Orm delete exception

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?

Upvotes: 6

Views: 5492

Answers (2)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236188

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();

Upvotes: 12

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

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.

Upvotes: 9

Related Questions