EF SqlQuery多映射

时间:2014-06-04 05:14:29

标签: c# entity-framework

EF SqlQuery 是否具有Dapper中的多映射功能?

请参阅:Fill list object using dapper c#

EF联接查询结果并不像我预期的那样。

示例:

型号:

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; }
}

数据库访问:

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);

SqlQuery的结果不包含Attachments,我不想获得OtherCollection1和OtherCollection2。在Dapper看来你可以改变它的映射。 在 Web API 中使用Include,将在延迟加载模式下调用OtherCollection1和OtherCollection2。而且我不需要它们。

1 个答案:

答案 0 :(得分:0)

如果您需要该功能来加载关联,那么EF可以使用Include方法获得内置支持。否则,没有用于多映射的功能。

相关问题