使用 SimpleRepository 的 Subsonic 3 Sql Join 无法连接具有相同名称的列?
我正在尝试使用 Subsonic Fluent 查询界面在两个表之间创建一个简单的内部联接:
[SearchJobResults]
PK SearchJobResultId int
名称字符串
描述字符串
[ParseResults]
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 的源代码以了解此执行的来源:
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);
}
我尝试使用 Aliases 但失败了。此外,如果我只做一个像这样的简单查询,它就可以正常工作:
var d = new Select(p).From("ParseResults").GetRecordCount();
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 = ProviderFactory.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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,您需要使用 From/Join 的类型化 T 重载才能使其正常工作。
Subsonic 中的 FromTables 集合现在可以正确枚举,因为它是从实际对象而不是数据库中读取类型。
Turns out you need to use the Typed T overloads of From/Join to get this working.
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.