c# – Insight.Database列映射到对象

我使用
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

最佳答案 这可以通过查询端的一些设置来实现.不确定属性是否可行.

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中的查询功能有多强大.

点赞