Petapoco的多重映射可以处理多个JOIN吗?

前端之家收集整理的这篇文章主要介绍了Petapoco的多重映射可以处理多个JOIN吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个列表对象,我用它填充使用Petapoco.

属性名称正在加工数据库模式.主类是Issue,它与另外两个类相关,名称属性也与数据库模式匹配:Condition和SeverityLevel.

public class Issue
{
    public int Id { get; set; } // Primary key

    /* Some properties... */
    public DateTime? CreatedOn { get; set; }
    public string CreatedBy { get; set; }
    public DateTime? ModifiedOn { get; set; }
    public string ModifiedBy { get; set; }

    /* ... */

    // Related source and target conditions
    [PetaPoco.Ignore]
    public Condition SourceCondition { get; set; }

    [PetaPoco.Ignore]
    public Condition TargetCondition { get; set; }

    // Related severity level
    [PetaPoco.Ignore]
    public SeverityLevel CurrentSeverityLevel { get; set; }
}

public class Condition
{
    public int Id { get; set; } // Primary Key
    public string Description  { get; set; }
}

public class SeverityLevel
{
    public int Id { get; set; } // Primary key
    public string Description { get; set; }
    public string HexColorDisplay { get; set; }
}

实际上,当我检索问题列表时,我使用多重映射功能使用单个命令检索问题列表和相关的SeverityLevel:

var Results = Db.Fetch<Issue,SeverityLevel,Issue>(
    (i,sl) => { 
        i.CurrentSeverityLevel = sl;
        return i;
    },"SELECT /* ..shortened.. */ FROM Issue " + 
    "LEFT JOIN SeverityLevel ON SeverityLevel.Id = Issue.SeverityLevelId " + 
    "WHERE Issue.Id=@0",issueId);

现在,因为Petapoco似乎没有处理多个JOINS,所以我需要做第二步,将SourceCondition和TargetCondition附加到我检索到的每个问题上.

为此,我可以:

>在foreach循环中,在Read之后附加Source和Target条件,
>或检索整个条件列表,然后使用相同类型的for-each附加到每个问题.

目前,我正在使用第二种解决方案,因为数据库中有一组有限的条件.

无论如何,这样做对我来说听起来有点沉重,因为它需要几乎与添加JOINED表一样多的查询.

我想知道我能不能做到这样的工作:

var Results = Db.Fetch</* ????? */>(
    /* ???? */
    "SELECT /* ..shortened.. */ FROM Issue " + 
    "LEFT JOIN SeverityLevel ON SeverityLevel.Id = Issue.SeverityLevelId " + 
    "LEFT JOIN Condition Con1 ON Con1.Id = Issue.SourceConditionId " + 
    "LEFT JOIN Condition Con2 ON Con2.Id = Issue.TargetConditionId " + 
    "WHERE Issue.Id=@0",issueId);

尊敬的Petapoco作者,亲爱的Petapoco用户,这是一种处理这个问题的方法吗?

我可以使用Dapper来处理这个问题(如果它可以…),appart我绝对想保留Petapoco用于我的更新/插入操作?

解决方法

这应该可以做到.
var Results = Db.Fetch<Issue,Condition,sl,c1,c2) => { 
        i.CurrentSeverityLevel = sl;
        i.SourceCondition = c1;
        i.TargetCondition = c2;
        return i;
    },"SELECT Issue.*,SeverityLevel.*,Con1.*,Con2.* FROM Issue " + 
    "LEFT JOIN SeverityLevel ON SeverityLevel.Id = Issue.SeverityLevelId " + 
    "LEFT JOIN Condition Con1 ON Con1.Id = Issue.SourceConditionId " + 
    "LEFT JOIN Condition Con2 ON Con2.Id = Issue.TargetConditionId " + 
    "WHERE Issue.Id=@0",issueId);

我没有测试过这个.
我也在努力实现自动化.

选择的列与参数类型的顺序非常重要.

原文链接:/mssql/79011.html

猜你在找的MsSQL相关文章