I am trying to use Dapper.Contrib to extend the functionality of the IDbConnection
interface with, amongst others, the .insert()
method.
To do so, I have followed the somewhat brief and scattered documentation here and here. In short, I have used NuGet
to add Dapper
and Dapper.Contrib
to my project, I have added using Dapper;
and using Dapper.Contrib;
at the top of my Repository class, and I am using System.Data.SqlClient.SqlConnection()
to create an IDbConnection
.
Still, my connection object does not have the extended methods available. For example, when trying to use the .insert()
method, I get the message:
'IDbConnection' C# does not contain a definition for 'Insert' and no extension method 'Insert' accepting a first argument of type could be found (are you missing a using directive or an assembly reference?)
This is in an ASP.NET Core 2.0 project using Razor Pages.
For completeness sake, you can find the Repository class below.
Maybe interesting to note, is that the using
lines for Dapper and Dapper.Contrib are grayed out...
Also, of course I have a (very minimalistic) Model Class for the TEST Entity, containing one parameter, TEST_COLUMN
, annotated with [Key]
.
using Dapper.Contrib;
using Dapper;
using Microsoft.Extensions.Configuration;
using TestProject.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
namespace TestProject.Repository
{
public class TEST_Repository
{
IConfiguration configuration;
public TEST_Repository(IConfiguration configuration)
{
this.configuration = configuration;
}
public void Insert()
{
using (var con = this.GetConnection())
{
con.Insert(new TEST { TEST_COLUMN = "test" });
}
}
public IDbConnection GetConnection()
{
return new SqlConnection(configuration.GetSection("ConnectionStrings").GetSection("DefaultConnection").Value);
}
}
}