【问题标题】:Subsonic 3 Sql Join using SimpleRepository can't join on columns with same name?Subsonic 3 Sql Join 使用 SimpleRepository 不能加入同名的列?
【发布时间】:2011-01-14 02:55:45
【问题描述】:

我正在尝试使用 Subsonic Fluent 查询界面在两个表之间创建简单的内部联接:

[搜索工作结果]

PK SearchJobResultId int
名称字符串
描述字符串

[解析结果]

PK ParseResultId int
名称字符串
SearchJobResultId int

这些表之间存在一对一的关系。

请记住,我没有使用 ActiveRecord。我有 ParseResult 和 SearchJobResult 的类可以正常工作。

 IDataProvider p  = ProviderFactory.GetProvider("DemacDB");
 SqlQuery query = new SqlQuery(p);            

 var q = new Select(p).From("ParseResults")
         .InnerJoin<SearchJobResult>("SearchJobResultId","SearchJobResultId").GetRecordCount();

这段代码抛出异常:

测试方法 Models.SearchTests.TestSubsonicQueryMethods 抛出异常:System.InvalidOperationException: 不知道要加入哪一列 - 在 ParseResults 表中找不到列 SearchJobResultId。

我查看了 SubSonic 的源代码以了解此执行的来源:

 private void CreateJoin<T>(string fromColumnName, string toColumnName, Join.JoinType type)
    {
        //see if we can find the table
        var toTable = _provider.FindOrCreateTable(typeof(T));

        //the assumption here is that the FromTable[0] is the table to join from
        if(FromTables.Count == 0)
            throw new InvalidOperationException("Can't join if there's no table to join to - make sure to use From() before InnerJoin");

        if(toTable == null)
            throw new InvalidOperationException("Can't find the table for this type. Try using the Column instead");

        var fromColumn = FromTables[0].GetColumn(fromColumnName);
        if(fromColumn == null)
            throw new InvalidOperationException("Don't know which column to join to - can't find column " + fromColumnName + " in table " + FromTables[0].Name);

        var toColumn = toTable.GetColumn(toColumnName);
        if(toColumn == null)
            throw new InvalidOperationException("Don't know which column to join to - can't find column " + toColumnName + " in table " + toTable.Name);

        CreateJoin(fromColumn, toColumn, Join.JoinType.Inner);
    }

我尝试过使用别名,但失败了。此外,如果我只是做一个这样的简单查询,它就可以正常工作:

var d = new Select(p).From("ParseResults").GetRecordCount();

【问题讨论】:

    标签: subsonic subsonic3


    【解决方案1】:

    事实证明,您需要使用 From/Join 的 Typed T 重载才能使其正常工作。

     var b = new Select(p).From<ParseResult>().InnerJoin<SearchJobResult>("SearchJobResultId", "SearchJobResultId").GetRecordCount();
    

    现在可以正确枚举 Subsonic 中的 FromTables 集合,因为它从实际对象而不是 DB 中读取类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多