【发布时间】: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();
【问题讨论】: