我有一个列表对象,我用它来填充使用Petapoco。
类属性和名称正在加工数据库模式。主要类是 问题 ,它与名称和属性与数据库模式匹配的另外两个类相关: 条件 < / strong>和 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 我检索到的每个问题。
要做到这一点,我可以:
目前,我正在使用第二种解决方案,因为数据库中有一组有限的条件。
无论如何,这样做对我来说听起来有点沉重,因为它需要几乎与添加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进行更新/插入操作?
答案 0 :(得分:13)
这应该可以做到。
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);
我没有测试过这个。 我也在努力实现自动化。
非常重要的是,所选列的顺序与参数类型的顺序相同。