1

I have this custom repo and when I try execute a query, it returns an exception If I execute the query using sql string, it does not return an error, but when I use some extention then I have this exception.

Like this:

public override IEnumerable<TableContrato> All()
{
    //var query = "select * from Contrato";
    //var data = Conn.Query<TableContrato>(query);
    var data = Conn.GetList<TableContrato>();
    return data;
}

All my entities are created in c# using "Table" Prefix, like TableContrato, and my table is called Contrato

This way, I've build a custom mapper, like this.

public class CustomMapper<TTableEntity>  : PluralizedAutoClassMapper<TTableEntity> where TTableEntity : class
{
    public override void Table(string tableName)
    {
        tableName = tableName.Replace("Table", string.Empty).Trim();
        base.Table(tableName);
    }
}

and this is my repo base

public abstract class ReadOnlyRepositoryBase<TEntity, TTable, TKey> : IReadOnlyRepository<TEntity, TKey>
    where TEntity : class where TTable : class
{
    protected IDbConnection Conn { get; set; }
    protected DapperContext Context { get; private set; }

    protected ReadOnlyRepositoryBase()
    {
        Context = new DapperContext();
        Conn = Context.Connection;
        InicializaMappings();
    }

    public void InicializaMappings()
    {
        global::DapperExtensions.DapperExtensions.DefaultMapper = typeof(CustomMapper<>);
    }
}

and here is my exception.

enter image description here

I know I could do all with literal queries, but this way I cannot use Expression trees for filtering, neither generics.

What I'm doing wrong?

EDIT: 26/05/2015 - TableContrato

public class TableContrato
{
    public Guid ContratoId { get; set; }

    public Guid EmpresaId { get; set; }

    public string ContratoNome { get; set; }

    public string ContratoCodigo { get; set; }

    public DateTime? DataDeCriacao { get; set; }

    public Guid? UsuarioQueCriou { get; set; }

    public TableEmpresaGrupo Empresa { get; set; }

    public virtual ICollection<TableLocal> Locais { get; set; }

}

UPDATE - 31/05/2016 - Sql profiler

Here is an image of Sql Server Profile, of executed Sql. Aparently, the '*' character is missed.

enter image description here

I think is a configuration error, so here is the map class

public class TableContratoMap : ClassMapper<TableContrato>
{
    public TableContratoMap()
    {
        // ReSharper disable once RedundantBaseQualifier
        base.Table("Contrato");


    }
}

One more doubt... I'm familiar with EF mapping, where I don't need to map every single column. Is it really neccessary in Dapper?

Jedi31
  • 735
  • 1
  • 6
  • 22
  • Can you post the query that it is saying there is a syntax error for? I know you say these are dynamic queries but could you have it output the text of each query it builds? – Tyler Benzing May 24 '16 at 21:28
  • Unfortunatelly no... I cannot capture generated sql query... I was thinking about that... so I could see the sql error... But, it didn't work... – Jedi31 May 25 '16 at 00:59
  • Could you please provide your `TableContrato` object? Your object schema may explain a lot about the problem you are having – Tyler Benzing May 25 '16 at 02:20
  • Are you sure the .Trim() isn't causing your query to look like `SELECT * FROMContracto WHERE` instead of `SELECT * FROM Contracto WHERE` – Tommy May 27 '16 at 00:07
  • I've never used Dapper myself but from what I have read; Does the `GetList` function require a predicate? – Tyler Benzing May 27 '16 at 00:38
  • @Tommy Wouldn't that mean that the parser wouldn't be able to generate the error it did? "Syntax error near FROM". If it were *FROMContrato* wouldn't it just think FROM was a part of the table name and not be able to discern it from Contrato? – Tyler Benzing May 27 '16 at 00:42
  • @UpAllNight - I have no idea, but given he is using a custom table name mapper, that would be the first place I would start debugging. It also could be doing something like `Select * From ContractoWHERE ` - that would generate the parser error. Since we can't see the generated SQL, one most likely would assume that the table name is messing the query up (since the error is around FROM). – Tommy May 27 '16 at 00:45
  • Edited with Sql Profiler info – Jedi31 May 31 '16 at 19:07

2 Answers2

1

You need to call AutoMap within your class mapper. Once you call that, AutoMap will build the collection of fields internally and apply that to the field list within the SQL statement.

public class TableContratoMap : ClassMapper<TableContrato>
{
public TableContratoMap()
   {
       // ReSharper disable once RedundantBaseQualifier
       base.Table("Contrato");
       AutoMap();

   }
}
G Davison
  • 1,079
  • 1
  • 14
  • 21
0

One more doubt... I'm familiar with EF mapping, where I don't need to map every single column. Is it really neccessary in Dapper?

No just use dapper contrib and same names in db as code.

Table Persons
Id
Name
Birth

Class Persons
Id
Name
Birth

var person = dapper.Get<Persons>(22);
David Ulvmoen
  • 87
  • 1
  • 3
  • You don't always have to map all your columns in DapperExtensions, but if you replace the default mapper then you need to do the basic tasks the original mapper would do. Contrib solves the requirements for CRUD methods a different way but requires attributes on the entity classes. – G Davison Sep 29 '16 at 13:11
  • You can use attribute, but you dont have to if yopu follow https://github.com/StackExchange/dapper-dot-net/tree/master/Dapper.Contrib – David Ulvmoen Sep 30 '16 at 12:06