Search code examples
c#dapperdapper-extensionsdapper-fluentmap

Dapper complex mapping Dapper.Extensions Dapper.FluentMap


I have a bit of a problem with my code and getting dapper to play nicely with it.

When I say my code it was inherited, so this is not my design.

I am trying to replace Entity Framework as the calls to the database are less than efficient so I wanted to be more in control of the SQL produced so Dapper seemed like the obvious choice.

The issue I am having is that I am struggling to map the poco classes I have to the dapper multi query.

The issue I have is as follows:

    public Feed GetFeedDapper(int feedId)
        {
            Feed feed = null;

            var sql =
            @"
            SELECT * FROM Feeds WHERE FeedId= @FeedId
            SELECT * FROM FeedFilterParameters WHERE FeedId = @FeedId
            SELECT * FROM TeamFeeds WHERE FeedId = @FeedId";

            using (var multi = DbConnection.QueryMultiple(sql, new { FeedId = feedId }))
            {
                feed = multi.Read<Feed>().Single();
                feed.Parameters = multi.Read<FeedFilterParameter>().ToList();
                feed.TeamFeeds = multi.Read<TeamFeed>().ToList();
            } 

            return feed;
        }

This is getting me the correct results from the database, which is fine, the issue is that not all properties on the Feed object are being mapped. The feed has a property called InboundProperties that is of type InboundProperties as shown below, and in the database they are stored as InboundProperties_{PropName}. These properties are not being mapped and nothing I try in DapperExtensions or FluentMap are working.

public class InboundProperties
{
    public string ExternalRef { get; set; }
    public string ExternalRefPrevious { get; set; }
    public string ExternalId { get; set; }
    public string ExternalName { get; set; }
    public string ExternalToken { get; set; }
    public int ExternalAPICounts { get; set; }
    public string ExternalLink { get; set; }
    public string ExternalPicture { get; set; }
    public string LastProcessedMessageId { get; set; }
    public long? LastProcessedMessageTime { get; set; }
    public DateTime? MessageCountStartDT { get; set; }
    public Int32 TenancyId { get; set; }
    public virtual int FeedScopeInt { get; set; }
}

Can anyone help me map these properties??


Solution

  • Have you tried sth like that:

    Feed feed = null;
    
    var sql =
    @"
    SELECT * FROM Feeds WHERE FeedId= @FeedId
    SELECT InboundProperties_ExternalRef as ExternalRef, InboundProperties_ExternalRefPrevious as ExternalRefPrevious FROM Feeds as InboundProperties WHERE FeedId= @FeedId
    SELECT * FROM FeedFilterParameters WHERE FeedId = @FeedId
    SELECT * FROM TeamFeeds WHERE FeedId = @FeedId";
    
    using (var multi = DbConnection.QueryMultiple(sql, new { FeedId = feedId }))
    {
        feed = multi.Read<Feed>().Single();
        feed.InboundProperties = multi.Read<InboundProperties>().Single();
        feed.Parameters = multi.Read<FeedFilterParameter>().ToList();
        feed.TeamFeeds = multi.Read<TeamFeed>().ToList();
    } 
    
    return feed;
    

    I mapped only 2 first properties, but if it works you will know how to map all properties.