0

I've been playing with Dapper-Contrib for a bit and having problem retrieving an object using .Get so that I can maintain change tracking for the object.

My Interface:

Imports Dapper.Contrib.Extensions

Public Interface ICountry

    <Key>
    Property CountryGUID As Guid
    Property Country As String

End Interface

The Class:

Imports Dapper.Contrib.Extensions

<Table("Country")>
Public Class Country
    Implements ICountry
    <Key>
    Public Property CountryGUID As Guid Implements ICountry.CountryGUID
    Public Property Country As String Implements ICountry.Country

End Class

My function:

   Public Function Find(countryId As Guid) As ICountry
        Dim country As ICountry

        Using conn = New SqlConnection(ConnectionString)
            conn.Open()

            country = conn.Get(Of ICountry)(countryId)

            conn.Close()
        End Using

        Return country
    End Function

And I got an error saying Invalid object name 'Countrys' when calling the above function.

The problem seems to be caused by Dapper sqlmapperextensions automatically adds ā€œsā€ to tablename and that can be solved by defining TableAttribute. However, TableAttribute is supported for classes only but not interfaces.

Please note that I have to call .Get with an interface to have tracking functionality.

0

I had exactly the same problem and managed to circumvent it.

Looking at the test suite Dapper Contrib, I found this code. So I created a function to modify the table mapping

private static void ConfigureTableMapper()
{
    if (SqlMapperExtensions.TableNameMapper != null)
        return;

    SqlMapperExtensions.TableNameMapper = (type) =>
    {
        var tableattr = type.GetCustomAttributes(false).SingleOrDefault(attr => attr.GetType().Name == "TableAttribute") as dynamic;
        if (tableattr != null)
            return tableattr.Name;

        var name = type.Name;
        if (type.IsInterface && name.StartsWith("I"))
            name = name.Substring(1);
        return name;
    };
}

It's C# code but should be easily converted to VB.

Hope this helps !

Your Answer

By clicking ā€œPost Your Answerā€, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.