henkmollema/Dommel

SQLExpression does not use column mapping

fernandocristan opened this issue · 2 comments

Hello, I have the following mapping

ToTable("pro_categoria");
Map(x => x.Cod).IsKey().IsIdentity().ToColumn("cod", false);
Map(x => x.Descricao).ToColumn("descricao", false);
Map(x => x.Ativo).ToColumn("ativo", false);

When performing a search using sqlExpression, columns are not mapped

_conn.From<Categoria>(x =>
x.Select(x => new { x.Cod, x.Descricao })
.Where(y => y.Cod == 1))

Generated sql = select "Cod", "Descricao" from "pro_categoria" where ("cod" = @p1)

As a workaraund you can create a Dto to select only the columns you need, and use this like this (preventing overselection):

  • Declare DTO
public record CategoriaDto()
{
    public string Cod{ get; init; } = default!;
    public string Descricao { get; init; } = default!;
}
  • Decalre Map for DTO
    public class CategoriaDtoMap : DommelEntityMap<CategoriaDto>
    {
        public CategoriaDtoMap ()
        {
            ToTable("pro_categoria");
            Map(x => x.Cod).IsKey().IsIdentity().ToColumn("cod", false);
            Map(x => x.Descricao).ToColumn("descricao", false);
        }
    }
  • Use DTO in query to only select the columns you need with the correct name mapping
  _conn.From<CategoriaDto>(x =>
  x.Select(x => new CategoriaDto())
  .Where(y => y.Cod == 1))

This will generate your SQL without overselecting and with correct mapping

  • PostgreSQL output example
SELECT "pro_categoria"."cod",
       "pro_categoria"."descricao"
FROM   "pro_categoria"
WHERE  ( "pro_categoria"."cod" = $1 ) 

Thanks!