【问题标题】:Can the multimapping of Petapoco handle multiple JOINs?Petapoco 的多重映射可以处理多个 JOIN 吗?
【发布时间】:2011-10-03 14:27:07
【问题描述】:

我有一个使用 Petapoco 填充的对象列表。

类属性和名称与数据库模式匹配。主类是Issue,它与另外两个名称和属性也与数据库模式匹配的类相关:ConditionSeverityLevel

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,我需要执行第二步来附加 SourceConditionTargetCondition 我检索到的每个问题。

为此,我可以:

  • 在读取之后,在 foreach 循环中附加源和目标条件,
  • 或检索整个条件列表,然后使用相同类型的 for-each 附加到每个问题。

目前,我正在使用第二种解决方案,因为数据库中的 Condition 集是有限的。

无论如何,这样做对我来说听起来有点沉重,因为它需要完成的查询几乎与添加的 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 来处理这个问题吗(如果可以的话......),我绝对希望保留 Petapoco 用于我的更新/插入操作?

【问题讨论】:

    标签: join mapping dapper petapoco


    【解决方案1】:

    这应该是可以做到的。

    var Results = Db.Fetch<Issue, SeverityLevel, Condition, Condition, Issue>(
        (i, 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);
    

    我没有测试过这个。 我也在研究一种自动化的方法。

    真的重要的是,所选列的顺序与参数类型的顺序相同。

    【讨论】:

    • 它的工作!非常感谢。我认为我们在这里达到了极限,因为我无法添加更多连接。
    • 有可能,您只需要使用非泛型重载,或者如果您使用的是 dotnet 4.0,您可以添加自己的重载,它需要更多参数。 4 是 .net 3.5 的极限。
    猜你喜欢
    • 1970-01-01
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    相关资源
    最近更新 更多