Pregunta

Say I have these 2 tables/domains

[TableName("TableA")]
    [PrimaryKey("TableAId")]
    public class TableA
    {
        public int TableAId { get; set; }
        public string City { get; set; }

        public TableB TableB { get; set; }

        public TableA()
        {
            TableB = new TableB();
        }

    }

    [TableName("TableB")]
    [PrimaryKey("TableBId")]
    public class TableB
    {
        public int TableBId { get; set; }
        public string Name { get; set; }
    }


var sql = @" Select TableA.*, TableB.*
FROM         TableA INNER JOIN
                      TableB ON TableA.TableBId = TableB.TableBID";

// peta poco
var result = db.Query<TableA, TableB>(sql);

When I do this the TableA.TableB gets filled and TableA id gets filled but City is null. I have to specify each column for it to bind. Is there away to just use the star instead of specifying each column?

¿Fue útil?

Solución

What you have there should work however, what I think might be happening is due to these conditions happening:

  1. You have a column TableBId on TableA
  2. You don't have a TableBId property on the class TableA
  3. When you run the query manually, City is coming after TableBId on the TableA.* part

The mapping works by running through the columns in order and the processing works by:

  1. If the column in on the first class in your generic parameter list it will get mapped and move on to the next column (still using the first class for mapping)
  2. If the column is not on the first class, then it will consider the first class finished and move to the second class and so on....
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top