Martin Milan
Martin Milan

Reputation: 6390

Multi Mapping in Dapper

Using dapper. when I evaluate this:

var trains =
                conn.Query<Train, TrainDriver, Train>(
                    "Select T.TrainId,T.OperatorId,T.TrainDescriptor,T.DriverId,TD.TrainDriverId,TD.Name,TD.Salary from trains as T" +
                    "Inner Join TrainDrivers as TD on (T.DriverId = TD.TrainDriverId)" +
                    "Where T.TrainId = @id", (t, td) =>
                    {
                        t.Driver = td;
                        return t;
                    }, new {id = id}, null, true, "TrainDriverId");

It seems I am getting a SQLException : {"The multi-part identifier \"T.DriverId\" could not be bound.\r\nThe multi-part identifier \"T.TrainId\" could not be bound.\r\nThe multi-part identifier \"T.TrainId\" could not be bound.\r\nThe multi-part identifier \"T.OperatorId\" could not be bound.\r\nThe multi-part identifier \"T.TrainDescriptor\" could not be bound.\r\nThe multi-part identifier \"T.DriverId\" could not be bound."}

These fields all exist, and if I strip out the SQL and run the query seperately in SQL Manager, it works...

Any thoughts?

Upvotes: 0

Views: 197

Answers (1)

Void Ray
Void Ray

Reputation: 10219

You are just missing some spaces in the SQL query. Try:

    var trains =
                    conn.Query<Train, Driver, Train>(
                        "Select T.TrainId,T.OperatorId,T.TrainDescriptor,T.DriverId,TD.TrainDriverId,TD.Name,TD.Salary from trains as T" +
                        " Inner Join TrainDrivers as TD on (T.DriverId = TD.TrainDriverId)" +
                        " Where T.TrainId = @id", (t, td) =>
                        {
                            t.Driver = td;
                            return t;
                        }, new { id = id }, null, true, "TrainDriverId");

Upvotes: 1

Related Questions