开发者

Subsonic 3 Sql Join using SimpleRepository can't join on columns with same name?

I'm trying to using the Subsonic Fluent query interface to create a simple inner join between two tables:

[SearchJobResults]

PK SearchJobResultId int

Name string

Desc string

[ParseResults]

PK ParseResultId int

Name string

SearchJobResultId int

There is a 1 to 1 relationship between these tables.

Keep in mind, I'm not using ActiveRecord. I have classes for ParseResult and SearchJobResult that work fine.

 IDataProvider p  = P开发者_开发知识库roviderFactory.GetProvider("DemacDB");
 SqlQuery query = new SqlQuery(p);            

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

This code throws an exception:

Test method Models.SearchTests.TestSubsonicQueryMethods threw exception: System.InvalidOperationException: Don't know which column to join to - can't find column SearchJobResultId in table ParseResults.

I've looked at the source code for SubSonic to see where this execption is coming from:

 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);
    }

I've tried using Aliases but that fails. Additionally, if I just do a simple query like this it works fine:

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


Turns out you need to use the Typed T overloads of From/Join to get this working.

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

Works as the FromTables collection in Subsonic now correctly gets enumerated because it's reading the type from the actual object and not the DB.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜