Insight.Database列映射到对象

4

我正在使用Insight.Database作为我们的微型ORM。 我想找出是否有一种方法将以下POCO类关联映射到单行结果中的这些对象。

public class Rule
{
    public int Id { get; set; }
    public string Name { get; set; }
    public RuleDetail Source { get; set; }
    public RuleDetail Destination { get; set; }
}

public class RuleDetail
{
    public int Id { get; set; }
    public Name { get; set; }
    public Date DateTime { get; set; }
    // omitted...
}

以下是我们存储过程返回的列:

Id
Name

// Should map to Source object.
SourceId
SourceName
SourceDateTime

// Should map to Destination object.
DestinationId
DestinationName
DestinationDateTime
2个回答

1
这需要在查询方面进行一些设置才能实现。不确定是否可以使用属性。
var returns = Query.Returns(
    new OneToOne<Rule, RuleDetail, RuleDetail>(
        callback: (rule, source, destination) => {
            rule.Source = source;
            rule.Destination = destination;
        },
        columnOverride: new ColumnOverride[] {
            new ColumnOverride<RuleDetail>("SourceId", "Id"),
            new ColumnOverride<RuleDetail>("SourceName", "Name"),
            new ColumnOverride<RuleDetail>("SourceDateTime", "DateTime"),
            new ColumnOverride<RuleDetail>("DestinationId", "Id"),
            new ColumnOverride<RuleDetail>("DestinationName", "Name"),
            new ColumnOverride<RuleDetail>("DestinationDateTime", "DateTime"),
        },
        splitColumns: new Dictionary<Type, string>() {
            { typeof(Rule), "Id" },
            { typeof(RuleDetail), "SourceId" },
            { typeof(RuleDetail), "DestinationId" },
        }
    )
);
return Db.Query(procedure, params, returns);

我不确定是否需要所有这些代码才能使其工作,但它确实展示了 Insight.Database 中查询功能的强大之处。

0

你可以尝试

public interface IRepo
{
    [Recordset(1, typeof(RuleDetail), into="Source", IsChild=true)]
    [Recordset(2, typeof(RuleDetail), into="Destination", IsChild=true)] 
    Rule GetFullyPopulatedRuleByIdStoredProcedure(int id);
}

您需要返回三个记录集 - 第一个[Recordset(0)]是您的规则,其余两个包含源和目标。我经常使用这种安排,它对我很有用。

如果您只返回单个记录集中的单行,则我不知道任何方法可以做到这一点。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接