1
votes

I'm trying to get specific data from db with this code

Bunts = _buntRepository.Bunts
            .Where(b => !steel.HasValue || b.SteelCode == steel)
            .OrderBy(b => b.CreationTime)
            .Skip((page - 1) * _pageSize)
            .Take(_pageSize)

Entity framework generates this SQL statement for it:

SELECT FIRST (5) SKIP (0)
"B"."BUNTCODE" AS "BUNTCODE", 
"B"."BUNTNAME" AS "BUNTNAME", 
"B"."BUNTDIAM" AS "BUNTDIAM", 
"B"."BUNTSTEEL" AS "BUNTSTEEL", 
"B"."BUNTCREATETIME" AS "BUNTCREATETIME"
FROM ( SELECT 
"D"."BUNTCODE" AS "BUNTCODE", 
"D"."BUNTNAME" AS "BUNTNAME", 
"D"."BUNTDIAM" AS "BUNTDIAM", 
"D"."BUNTSTEEL" AS "BUNTSTEEL", 
"D"."BUNTCREATETIME" AS "BUNTCREATETIME"
FROM "BUNTS" AS "D"
WHERE (("D"."BUNTSTEEL" = @p__linq__1) OR (("D"."BUNTSTEEL" IS NULL) AND (@p__linq__1 IS NULL)))
)  AS "B"
ORDER BY "B"."BUNTCREATETIME" ASC

This code throws exception with text

Dynamic SQL Error.
SQL error code = -804.
Data type unknown.

Same does IBExpert. Seems like something wrong with @p__linq__1 parameter. How do I solve this problem?

I'm using Firebird 2.1.6.18547

1

1 Answers

0
votes

The generated uses @p__linq__1 IS NULL (which is actually sent as ? IS NULL to the server), and this was only introduced in Firebird 2.5.

To be able to use this you either need to upgrade to Firebird 2.5, or handle the steel.HasValue condition in code and not in the query as:

var query _buntRepository.Bunts
if (steel.HasValue) 
{
    query = query.Where(b => b.SteelCode == steel)
}
Bunts = query.OrderBy(b => b.CreationTime)
    .Skip((page - 1) * _pageSize)
    .Take(_pageSize);

NOTE: The above doesn't confirm to the generated query (which seems to be a bug). If you want the result as generated then you need:

if (steel.HasValue) 
{
    query = query.Where(b => b.SteelCode == steel.Value)
}
else 
{
    query = query.Where(b => b.SteelCode == null)
}