【问题标题】:Insight.Database column mapping to objectInsight.Database 列映射到对象
【发布时间】:2016-10-14 13:38:34
【问题描述】:

我使用Insight.Database 作为我们的micro-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

【问题讨论】:

    标签: c# sql orm insight.database


    【解决方案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 中的查询功能有多么强大。

    【讨论】:

      【解决方案2】:

      你可以试试

      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)] 是您的规则,另外两个包含源和目标。我经常使用这种安排,它很适合我。

      如果您在单个记录集中返回单行,我不知道有什么方法可以做到这一点。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-27
        • 2019-12-06
        • 1970-01-01
        • 1970-01-01
        • 2013-03-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多