我正在尝试使用Subsonic Fluent查询界面在两个表之间创建一个简单的内部联接:
PK SearchJobResultId int
名称字符串
描述字符串
PK ParseResultId int
名称字符串
SearchJobResultId int
这些表之间存在1对1的关系。
请记住,我没有使用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的源代码,看看这个execption的来源:
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();
答案 0 :(得分:1)
事实证明,您需要使用From / Join的Typed T重载来实现此功能。
var b = new Select(p).From<ParseResult>().InnerJoin<SearchJobResult>("SearchJobResultId", "SearchJobResultId").GetRecordCount();
现在可以正确地枚举Subsonic中的FromTables集合,因为它从实际对象而不是数据库中读取类型。