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?
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.
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 .
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.
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();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With