Hi There,
I am having a problem with queries that inspect navigation properties testing for null.
Imagine I have two tables
PriceBand
PriceBandId (Guid - PK)
Name (Nvarchar 50)
PriceBandLink
PriceBandId (Guid - PK)
BasedOnPriceBandId (Guid - FK)
Next I add a EntityModel and import these two tables into the model
Now the PriceBand entity will have two navigation properties;
1. Multiplicity * that references child price band objects.
2. Multiplicity 0..1 that references the "parent" price band if any.
If I use the default code generation then the following code works (again assuming the table actually contains some data!)
```
using(var scope = new TestModelContainer())
{
var query = scope.PriceBands.Where((item)=>item.BasedOnPriceBand == null);
Assert.IsTrue(query.Any(), "No root price-bands found.");
}
```
If I use the EF T4 templates and the generated code uses the EF6 DbContext (with proxy generation and lazy loading enabled) then the query stops working.
The SQL profiler shows the following;
.NET4.5 ObjectContext uses this query;
```
SELECT
CASE WHEN ( EXISTS (SELECT
1 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable2]
WHERE 1 = 0
)) THEN cast(1 as bit) WHEN ( NOT EXISTS (SELECT
1 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable3]
WHERE 1 = 0
)) THEN case (0 as bit) END AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
```
Whereas EF6 DbContext uses this query;
```
SELECT
CASE WHEN ( EXISTS (SELECT
1 AS [C1]
FROM [dbo].[zc_PriceBand] AS [Extent1]
LEFT OUTER JOIN [dbo].[zc_PriceBandLink] AS [Extent2] ON [Extent1].[PriceBandId] = [Extent2].[PriceBandId]
LEFT OUTER JOIN [dbo].[zc_PriceBand] AS [Extent3] ON [Extent2].[BasedOnPriceBandId] = [Extent3].[PriceBandId]
WHERE [Extent3].[PriceBandId] IS NULL
)) THEN cast(1 as bit) WHEN ( NOT EXISTS (SELECT
1 AS [C1]
FROM [dbo].[zc_PriceBand] AS [Extent4]
LEFT OUTER JOIN [dbo].[zc_PriceBandLink] AS [Extent5] ON [Extent4].[PriceBandId] = [Extent5].[PriceBandId]
LEFT OUTER JOIN [dbo].[zc_PriceBand] AS [Extent6] ON [Extent5].[BasedOnPriceBandId] = [Extent6].[PriceBandId]
WHERE [Extent6].[PriceBandId] IS NULL
)) THEN cast(0 as bit) END AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
```
Have I missed something or is there some incompatibility in the edmx information between .NET4.5 and EF6?
Hope you can help,
Regards,
Adrian Lewis
I am having a problem with queries that inspect navigation properties testing for null.
Imagine I have two tables
PriceBand
PriceBandId (Guid - PK)
Name (Nvarchar 50)
PriceBandLink
PriceBandId (Guid - PK)
BasedOnPriceBandId (Guid - FK)
Next I add a EntityModel and import these two tables into the model
Now the PriceBand entity will have two navigation properties;
1. Multiplicity * that references child price band objects.
2. Multiplicity 0..1 that references the "parent" price band if any.
If I use the default code generation then the following code works (again assuming the table actually contains some data!)
```
using(var scope = new TestModelContainer())
{
var query = scope.PriceBands.Where((item)=>item.BasedOnPriceBand == null);
Assert.IsTrue(query.Any(), "No root price-bands found.");
}
```
If I use the EF T4 templates and the generated code uses the EF6 DbContext (with proxy generation and lazy loading enabled) then the query stops working.
The SQL profiler shows the following;
.NET4.5 ObjectContext uses this query;
```
SELECT
CASE WHEN ( EXISTS (SELECT
1 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable2]
WHERE 1 = 0
)) THEN cast(1 as bit) WHEN ( NOT EXISTS (SELECT
1 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable3]
WHERE 1 = 0
)) THEN case (0 as bit) END AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
```
Whereas EF6 DbContext uses this query;
```
SELECT
CASE WHEN ( EXISTS (SELECT
1 AS [C1]
FROM [dbo].[zc_PriceBand] AS [Extent1]
LEFT OUTER JOIN [dbo].[zc_PriceBandLink] AS [Extent2] ON [Extent1].[PriceBandId] = [Extent2].[PriceBandId]
LEFT OUTER JOIN [dbo].[zc_PriceBand] AS [Extent3] ON [Extent2].[BasedOnPriceBandId] = [Extent3].[PriceBandId]
WHERE [Extent3].[PriceBandId] IS NULL
)) THEN cast(1 as bit) WHEN ( NOT EXISTS (SELECT
1 AS [C1]
FROM [dbo].[zc_PriceBand] AS [Extent4]
LEFT OUTER JOIN [dbo].[zc_PriceBandLink] AS [Extent5] ON [Extent4].[PriceBandId] = [Extent5].[PriceBandId]
LEFT OUTER JOIN [dbo].[zc_PriceBand] AS [Extent6] ON [Extent5].[BasedOnPriceBandId] = [Extent6].[PriceBandId]
WHERE [Extent6].[PriceBandId] IS NULL
)) THEN cast(0 as bit) END AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
```
Have I missed something or is there some incompatibility in the edmx information between .NET4.5 and EF6?
Hope you can help,
Regards,
Adrian Lewis