使用 SimpleRepository 的 Subsonic 3 Sql Join 无法连接具有相同名称的列?

发布于 2024-08-20 05:04:09 字数 2178 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蘸点软妹酱 2024-08-27 05:04:09

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文