0

Does EF SqlQuery has Multi-Mapping feature something like in Dapper?

See: Fill list object using dapper c#

EF join queries results are not as I expected.

Example:

Models:

public class Tlb
{
    [Key]
    public int TlbId { get; set; }
    public bool IsPublished { get; set; }
    public virtual ICollection<TlbAttachment> Attachments { get; set; }
    public virtual ICollection<TlbAttachment> OtherCollection1 { get; set; }
    public virtual ICollection<TlbAttachment> OtherCollection2 { get; set; }
}

public class TlbAttachment
{
    [Key, Column(Order = 0)]
    public int AttachmentId { get; set; }

    [ForeignKey("AttachmentId")]
    public virtual Attachment Attachment { get; set; }

    [Key, Column(Order = 1)]
    public int TlbId { get; set; }

    [ForeignKey("TlbId")]
    public virtual Tlb Tlb { get; set; }
}

public class Attachment
{
    [Key]
    public int AttachmentId { get; set; }
    public int Type { get; set; }
}

DB access:

const string query = "SELECT * " +
                     "FROM Tlb AS t LEFT OUTER JOIN "+
                     "TlbAttachment AS at ON at.TlbId = t.TlbId LEFT OUTER JOIN "+
                     "Attachment AS a ON a.AttachmentId = at.AttachmentId " +
                     "WHERE t.IsPublished = 1 AND a.Type = 0";

return _DbContext.Database.SqlQuery<Model.Tlb>(query);

The result of SqlQuery doesn't contain Attachments and I don't want to get OtherCollection1 and OtherCollection2. Which seems in Dapper you can change its mapping. Using Include in Web API, both OtherCollection1 and OtherCollection2 will be called in lazy-loading mode. And I don't need them.

Community
  • 1
  • 1
Babak
  • 3,716
  • 6
  • 39
  • 56

1 Answers1

0

If you need that feature to load associations, then EF has build-in support by using Include method. Otherwise there is no such functionality for multi-mapping.

Vladimir Sachek
  • 1,126
  • 1
  • 7
  • 20
  • Your particular query is quite basic one which can be rewritten in LINQ. Here is an example how to use LEFT OUTER JOIN in LINQ http://stackoverflow.com/questions/3404975/left-outer-join-in-linq. If you don't need OtherCollections then don't specify them using Include method, but specify Attachments only. – Vladimir Sachek Jun 04 '14 at 09:35