0

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?

1
  • If I profile the server, it seems it tries to use the sp_executesql stored procedure, and it seems to be THAT that is having the issues... Commented May 3, 2016 at 15:23

1 Answer 1

1

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"); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks... That, together with changing some of the fields in the projection, seems to have done the trick...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.