0

Currently I'm working on a little project with .NET Core and PostgreSQL. For my data acccess, I would like to use Dapper.Contrib. Right now, I'm facing the following problem:

The following snippet shows the CREATE_DATABASE.sql

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

CREATE TABLE posts (
    id UUID DEFAULT uuid_generate_v4 (),
    created BIGINT,
    value VARCHAR NOT NULL,
    PRIMARY KEY (Id)
);

And the following is my model class in .NET Core

[Table("posts")]
public class Post
{
    [Column("id")]
    public Guid Id { get; set; }

    [Column("created")]
    public long Created { get; set; }

    [Column("value")]
    public string Value { get; set; }
}

The insert method looks like this (it is in the base class. In this case T is type of Post):

public void Insert(T entity)
{
    _connection.Insert(entity);
}

When trying to insert an Post object, I'm running into following error:

Ein Ausnahmefehler des Typs "Npgsql.PostgresException" ist in System.Private.CoreLib.dll aufgetreten.: '42703: column "Created" of relation "posts" does not exist

When set NpgsqlLogManager.Provider to new ConsoleLoggingProvider(NpgsqlLogLevel.Trace, true, true); there is the following INSERT statement logged:

DEBUG [14313] Executing statement(s):
        insert into posts ("Created", "Value") values ($1, $2) RETURNING Id

So, where is the problem here? I thought Dapper.Contrib is doing fine with attributes, but obviously it isn't.

Does anybody know how to fix this issue, except of rename the model properties into lower case?

Thank you so far.

MarcoB
  • 51
  • 2
  • 6

2 Answers2

0

It seems that Dapper.Contrib does not allow to specify a Column attribute (but it does allow to specify a Table attribute). At ticket is open for this case. But it does support custom column to property mappers.

See this post for more details on how to implement this mapping.

Dampypena
  • 26
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 07 '21 at 16:54
0

With Dapper.Contrib, there are ways to map table name but apparently, there is no way to map the column name. This feature was planned for 2.x version. Version 2.x is released; but the issue is still open. Looking at the release notes, feature is not yet added.

Please refer to this answer to know about other query generators for Dapper those support column name mapping.

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141