Question

I observe a strange behavior when querying for null field an Entity Framework entity with the Devart DotConnect for PostgreSQL connector.

For example: If I made the 2 following tests:

var test1 = context.blocs.Where(x => x.id_bloc == null);

var test2 = context.blocs.Where(x => x.id_bloc == 100);

When checking for EF auto-generated SQL query I obtain the following results:

Result for test1:

{SELECT 
 CAST(NULL AS int) AS "C1",
 CAST(NULL AS varchar) AS "C2",
 CAST(NULL AS varchar) AS "C3",
 CAST(NULL AS varchar) AS "C4"
FROM  ( SELECT 1 AS X) AS "SingleRowTable1"
WHERE true = false}

Result for test2:

{SELECT 
"Extent1".id_bloc,
"Extent1".numero,
"Extent1".nom,
"Extent1".titre
FROM "role".bloc AS "Extent1"
WHERE "Extent1".id_bloc = 100}

The result when querying for null field is very strange... I was expecting a result similar to the test 2 result but with an "Extent1".id_bloc IS NULL WHERE clause...

Is it a bug? How can I effectively query for null field?

Was it helpful?

Solution

Is the id_bloc property defined with the Entity Key attribute? If yes, the result SQL is generated correctly.

Entity Key cannot be nullable that's why Entity Framework avoids unnecessary request to the table: generates fake SQL which gives the same empty result set but does not eat server resources.

You can check the behaviuor with SQL Server (via System.Data.SqlClient) - it must be the same.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top