My question is base upon the following schema:
public class EntityA
{
// Primary Properties
public Guid ID { get; set; }
public string Name { get; set; }
// Navigation Properties
public LinkingEntity LinkingEntity { get; set; }
}
public class EntityB
{
// Primary Properties
public Guid ID { get; set; }
public string Name { get; set; }
// Navigation Properties
public LinkingEntity LinkingEntity { get; set; }
}
public class LinkingEntity
{
// Primary Properties
public Guid ID { get; set; }
public Guid EntityAID { get; set; }
public Guid EntityBID { get; set; }
// Navigation Properties
public EntityA EntityA { get; set; }
public EntityB EntityB { get; set; }
}
The following is the dapper code:
string sql = @"SELECT
[EntityA].[ID],
[EntityA].[Name],
[LinkingEntity].[ID],
[LinkingEntity].[EntityAID],
[LinkingEntity].[EntityBID],
[EntityB].[ID],
[EntityB].[Name]
FROM
[dbo].[EntityA]
JOIN
[dbo].[LinkingEntity] ON [EntityA].[ID] = [LinkingEntity].[EntityAID]
JOIN
[dbo].[EntityB] ON [LinkingEntity].[EntityBID] = [EntityB].[ID];";
using (var connection = CreateConnection())
{
return await connection.QueryAsync<EntityA, LinkingEntity, EntityB, EntityA>(
sql: sql,
(entityA, LinkingEntity, entityB) =>
{
if(entityA.LinkingEntities == null)
{
entityA.LinkingEntities = new List<LinkingEntity>();
}
LinkingEntity.EntityB = entityB;
entityA.LinkingEntities.Add(LinkingEntity);
return entityA;
}
);
}
The problem is that I am only getting one entityB coming through when I know there is multiple EntityBs for each EntityA.
My question is that how do i construct a dapper statement to populate the make sure that the set of EntityB is populated?