Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dapper Orm delete exception

Tags:

c#

dapper

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?

like image 658
ftdeveloper Avatar asked Nov 21 '13 10:11

ftdeveloper


People also ask

Does dapper use SqlClient?

SqlClient and SqlConnection , but Dapper supports other databases that use the DbConnection abstraction. We also need a projection class representing the results of our SQL query.

Why we use Dapper in. net core?

Dapper is an ORM (Object-Relational Mapper) or to be more precise a Micro ORM, which we can use to communicate with the database in our projects. By using Dapper, we can write SQL statements as if we would do it in the SQL Server. Dapper has great performance because it doesn't translate queries that we write in .

What is Dapper in asp net core?

Dapper is a NuGet library that can be added to any project. It extends the IDbConnection interface. The IDbConnection interface represents an open connection to data source implemented by the . NET framework. Every database provider extends this interface to for their database i.e. SQL Server, Oracle, MySQL etc.


2 Answers

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();
like image 69
Sergey Berezovskiy Avatar answered Oct 08 '22 06:10

Sergey Berezovskiy


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.

like image 21
Mike Perrenoud Avatar answered Oct 08 '22 05:10

Mike Perrenoud