Hi all,
I have a strange problem dealing with Entity Framework.
This problem never ever occured to me before, so I am kind of surprised.
It's all about the
AgencyId
, which is in fact null.
I verify this where I set
agencyEqualsNull
in the first line (it is true).
bool agencyEqualsNull = AgencyId == null;
tblTest test = context.tblTest.FirstOrDefault(w =>
w.couponId == Id
&&
w.couponCode == Code
&&
w.customerId == CustomerId
&&
w.agencyId == AgencyId
);
if (test != null)
{
}
tblTest test2 = context.tblTest.FirstOrDefault(w =>
w.couponId == Id
&&
w.couponCode == Code
&&
w.customerId == CustomerId
&&
w.agencyId == null
);
if (test2 != null)
{
}
Now for the first try I receive null for
test
, for the second try I recieve a value for
test2
(not null).
The only difference is that I do not pass the variable (which is in fact null) but null directly...
Do I overlook something? I ssumed the queries are both equal and should return a value...
// UPDATE
I fired up the SQL Server Profiler in order to enlight the situation
here is the query:
exec sp_executesql N'SELECT TOP (1)
[Extent1].[id] AS [id],
[Extent1].[code] AS [code],
[Extent1].[customerId] AS [customerId],
[Extent1].[employeeId] AS [employeeId],
FROM [dbo].[tblTest] AS [Extent1]
WHERE
([Extent1].[Id] = @p__linq__0) AND
([Extent1].[Code] = @p__linq__1) AND
([Extent1].[customerId] = @p__linq__2) AND
([Extent1].[employeeId] = @p__linq__3)',
N'@p__linq__0 bigint,
@p__linq__1 nvarchar(4000),
@p__linq__2 bigint,
@p__linq__3 bigint',
@p__linq__0=82,
@p__linq__1=N'ADVENT2012-CU88888',
@p__linq__2=88888,
@p__linq__3=NULL
In the last line you can see
@p__linq__3=NULL
If I pass
null
directly it resolves to
([Extent1].[employeeId] IS NULL)
instead of
([Extent1].[employeeId] = @p__linq__3)
where
@p__linq__3=NULL
Now I am even more puzzled because I really assumed the generated query would be the same if I pass a variable containing null or null directly...
// UPDATE
I figured out that this will do the trick:
change
w.agencyId == AgencyId
to
(AgencyId == null) ? w.agencyId == null : w.agencyId == AgencyId
I somehow assumed that this would be done automatically...
This is actually not my code and now I am kind of afraid that I have to check everything that might be related to this problem in the complete codebase...
Does anybody has a suggestion regarding this one?
Any help is kindly appreciated,
best regards
Andy