I have these classes and their equivalent tables in my database:
public class Entity
{
public int Id { get; set; }
public List<EntityIdentifier> Identifiers { get; set; }
public BaseEntity()
{
Identifiers = new List<EntityIdentifier>();
}
}
public class EntityIdentifier
{
public int Id { get; set; }
public int EntityId { get; set; }
public string Code { get; set; }
public string Value { get; set; }
}
I want to query the database with Dapper and automap the data.
I have this example of multi mapping, from the Dapper git page:
var sql =
@"select * from #Posts p
left join #Users u on u.Id = p.OwnerId
Order by p.Id";
var data = connection.Query<Post, User, Post>(sql, (post, user) => { post.Owner = user; return post;});
var post = data.First();
post.Content.IsEqualTo("Sams Post1");
post.Id.IsEqualTo(1);
post.Owner.Name.IsEqualTo("Sam");
post.Owner.Id.IsEqualTo(99);
However, in this example, each child (post) has a link to its parent (user). In my case, it is the parent (entity) that points to a list of children (identifiers).
How do I need to adapt the code to my case?
Here is the SQL query that I am using:
SELECT e.*, i.*
FROM Entity e INNER JOIN EntityIdentifier i ON i.EntityId = e.Id