NHibernate 3CR1: Conversion to Sql, generates sql syntax problem of unnecessary brackets

二次信任 提交于 2019-12-11 07:32:49

问题


My database schema is described below:

Form <-> Log

<--->>Seller1

<--->>Seller2

<--->>Seller3

I have a major entity (Form), one to one relationship to another object (Log) And one to many relationship to the childs (Sellers).

I want to pull out all the Forms that one of their Sellers meets certain conditions.

Unfortunately I'm having some problems:

If I run the following routine:

        [Test]
    public void Can_Get_Forms_Where_CorporationNumber_Is_510778087_Metohd0()
    {
        var CorporationNumber = "513514950";

        var list0 = formRepository
                                .Where(x => x.Sellers.Any(y => y.CorporationNumber == CorporationNumber))
                                .Fetch(x=> x.Sellers)
                                .Fetch(x => x.Log)
                                .Take(10).ToList();

        CollectionAssert.IsNotEmpty(list0);
    }

Then I will get sql syntax error

Incorrect syntax near ','

Using NHProf I took the query and found the problem. I reduced my query to Count query to focus on the problem:

select
 TOP (
    10 /* @p0 */)
 cast(
    count(
        *)
     as INT)
 as col_0_0_ 
from BillOfSaleForm form0_ 
where exists (
    select
         (
            sellers1_.FormID,
             sellers1_.tdNum)

from BillOfSaleSeller sellers1_ 
where form0_.FormID=sellers1_.FormID and
     (
        (
            sellers1_.MisparTagid is null)
         and
             (
                '513514950' /* @p1 */ is null)
             or
                 sellers1_.MisparTagid='513514950' /* @p1 */)
            )

We notice that section

    select
     (
        sellers1_.FormID,
         sellers1_.tdNum)

Has Extra brackets!.

Of course, if we remove the brackets query executed.

来源:https://stackoverflow.com/questions/4274678/nhibernate-3cr1-conversion-to-sql-generates-sql-syntax-problem-of-unnecessary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!