Dapper > Multi mapping fails to map bit to boolean properly

dapper multi-mapping

Question

I have the following code:

        string sql = "SELECT * FROM orders o " +
                     "INNER JOIN users u ON o.user_id = u.id " +
                     "WHERE o.ticker = @ticker AND o.user_id = @user_id";

        var _orders = connection.Query(sql, new { ticker = ticker, user_id = userId });

        IEnumerable<Order> orders = connection.Query<Order, User, Order>(sql,
                                        (o, u) =>
                                        {
                                            u.Id = u.Id.Trim();
                                            Order order = new Order(o.Ticker.Trim(), u, o.Type, o.Quantity, o.Price, o.IsMarketOrder, o.ExpirationDate, o.FillCompletely, o.FillImmediately);
                                            return order;
                                        },
                                        new { ticker = ticker, user_id = userId }
                                        );

        return orders.ToArray<Order>();

The two "connection.Query" execute the same SQL. The difference is that the first one returns a list of DapperRows whereas the second one is a generic Query<> that multi maps the entity automatically.

Now, in the first .Query() (that returns a list of DapperRows) the boolean properties (bit in the dB) "FillImmediately" and "FillCompletely" are true or false in the class depending if they are "1" or "0" in the DB. So that works fine.

However, when using the .Query<>(), both properties are always "false". It seems that the .Query<>() can't map the bit to boolean properly.

I'm using latest version of Dapper as of today (1.42) and SQL Server 2008

Thoughts?

Popular Answer

This seems to be a bug of multi-mapping to dapper. Try to put the ids at the end of select, after all the other columns that you want to map. For example:

SELECT o.Status, u.Name, u.FillCompletely, u.FillImmediately, -- the trick ids in the end -> u.user_id, o.order_id FROM orders o INNER JOIN users u ON o.user_id = u.id WHERE o.ticker = @ticker AND o.user_id = @user_id


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why