Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dapper.Contrib methods unavailable for IDbConnection object

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);
        }
    }
}
like image 720
Wouter Avatar asked Dec 23 '22 07:12

Wouter


1 Answers

The Insert method you are looking for lives inside of the Dapper.Contrib.Extensions namespace, as can be seen in the source, included for completeness:

namespace Dapper.Contrib.Extensions
{
    ...

    public static long Insert<T>(this IDbConnection connection, ...)

    ...
}

Hence, in order to use the Extension methods, you should add the following line to your code:

using Dapper.Contrib.Extensions;
like image 125
Kirk Larkin Avatar answered Apr 19 '23 23:04

Kirk Larkin