I have an application object which has many connection objects. This is great in the .Net world but unfortunately the join is managed via a join table with a composite key. Is it possible to map this in dapper using a multi-mapping approach?
For now I am just using two seperate sql statements, but it doesn't sit well with me ( coming from the EF side of things ). Here is some code I tried but could not get to work...
public class Application
{
public int ApplicationID { get; set; }
public string Name { get; set; }
public string Prefix { get; set; }
public List<Connection> Connections { get; set; }
}
public class ApplicationConnection
{
public int ApplicationID { get; set; }
public int ConnectionID { get; set; }
}
public class Connection
{
public int ConnectionID { get; set; }
public string ConnectionString { get; set; }
}
var tsql = @"SELECT A.*,C.*
FROM [Application] A
INNER JOIN [ApplicationConnections] AC on A.ApplicationID = AC.ApplicationID
INNER JOIN [Connections] C ON AC.ConnectionID = C.ConnectionID
WHERE A.ApplicationID = 122";
var results = connection.Query<Application, List<Connection>, Application>(tsql,
(appl, conn) => {
appl.Connections = conn; return appl;
},
splitOn: "ApplicationID,ConnectionID").First();
The error i keep getting is the "use splitOn" error, but I'm sure that is just the tip of the iceberg here. If anyone out there has any ideas, I'm all ears.