henkmollema / Dapper-FluentMap

Provides a simple API to fluently map POCO properties to database columns when using Dapper.
MIT License
429 stars 88 forks source link

Property name transform configuration not working #91

Closed grandsilence closed 4 years ago

grandsilence commented 5 years ago

Hi. I'm trying to convert MyCustomProperty to my_custom_property column (snake case) globally (for Postgresql). But Convention not applying to my model or even assembly namespaces.

Initialization:

public class Startup
{
        public void ConfigureServices(IServiceCollection services)
        {
           FluentMapper.Initialize(config => {
                config.AddConvention<PropertyTransformConvention>()
                    .ForEntity<Bot>();

                // manual mapping working fine
                // config.AddMap(new BotMap());
                config.ForDommel();
            });
        }
}

My Convention:

public class PropertyTransformConvention : Convention
    {
        public PropertyTransformConvention()
        {
            Properties()
                .Configure(c => c.Transform(s => s.ToSnakeCase()));
        }
    }

ToSnakeCase() working well.

Result SQL:

insert into Bots (Token, Login, Enabled, Comment, TemplateStart) values (@Token, @Login, @Enabled, @Comment, @TemplateStart) RETURNING Id

Expected SQL:

insert into Bots (token, login, enabled, comment, template_start) values (@Token, @Login, @Enabled, @Comment, @TemplateStart) RETURNING id

PropertyTransformConvention constructor called but column names stays unchanged. Thank you.

henkmollema commented 5 years ago

My guess is that mixing manual mapping and conventions does not work. Can you try it without configuring the manual mapped?

grandsilence commented 5 years ago

Without manual mapped it has the same behavior

StefanJanssen95 commented 5 years ago

I have the same issue, did you have any luck solving it?

The project it is in is open source and I pushed it in the state with the error so it can be looked at: https://github.com/StefanJanssen95/steam-web-api-wrapper/tree/2fe3d1c16d9cc9b12477f3d251a16dd4fc53d4e4

I hope you don't mind dirty code, I have not yet spend any time on the architecture. EvilRabbot.Data is the class library project which is calling the database and has the mappings in the Context class which is called from EvilRabbot.Bot

The following is the SQL to create the database

CREATE SCHEMA EvilRabbot

CREATE TABLE config_settings
(
    id    SERIAL PRIMARY KEY NOT NULL,
    name  TEXT               NOT NULL,
    value TEXT               NOT NULL
)

CREATE TABLE users
(
    id                  SERIAL PRIMARY KEY NOT NULL,
    snowflake_id        BIGINT             NOT NULL,
    last_known_username TEXT               NOT NULL,
    last_message_date   TIMESTAMP          NOT NULL
)
StefanJanssen95 commented 5 years ago

I found out why my code was not working. For retrieving data I used my own SQL function which was executed by Dapper itself. For updating I was using the Dapper.Contrib function which does not use the configured column names.

henkmollema commented 4 years ago

This should be fixed in https://www.nuget.org/packages/Dapper.FluentMap.Dommel/1.7.1.