|
| 1 | +# GQLEF007: Identity projection with abstract navigation access |
| 2 | + |
| 3 | +## Cause |
| 4 | + |
| 5 | +A filter uses identity projection (`_ => _` or 4-parameter syntax) but the filter lambda accesses properties through an abstract navigation type. |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +Abstract types cannot be instantiated in SQL projections. When Entity Framework Core encounters a projection that would require instantiating an abstract class, it cannot generate the SQL expression. Instead, EF Core falls back to using `Include()` which loads **all columns** from the related table. |
| 10 | + |
| 11 | +This creates a significant performance problem: |
| 12 | +- The filter only needs 1-2 fields from the navigation |
| 13 | +- But EF Core loads all 30+ fields because of the Include fallback |
| 14 | +- Query performance degrades and memory usage increases unnecessarily |
| 15 | + |
| 16 | +## Rule Details |
| 17 | + |
| 18 | +This diagnostic is reported when: |
| 19 | +1. A filter uses identity projection (explicit `_ => _` or implicit via 4-parameter syntax) |
| 20 | +2. The filter lambda accesses a property through a navigation property |
| 21 | +3. That navigation property's type is abstract (has the `abstract` keyword) |
| 22 | + |
| 23 | +### Examples of violations |
| 24 | + |
| 25 | +```csharp |
| 26 | +// ❌ Identity projection with abstract navigation access |
| 27 | +public abstract class BaseEntity |
| 28 | +{ |
| 29 | + public string Property { get; set; } |
| 30 | +} |
| 31 | + |
| 32 | +public class Child |
| 33 | +{ |
| 34 | + public Guid Id { get; set; } |
| 35 | + public BaseEntity Parent { get; set; } |
| 36 | +} |
| 37 | + |
| 38 | +// This triggers GQLEF007: |
| 39 | +filters.For<Child>().Add( |
| 40 | + projection: _ => _, // Identity projection |
| 41 | + filter: (_, _, _, c) => c.Parent.Property == "test"); |
| 42 | + |
| 43 | +// This also triggers GQLEF007 (4-parameter syntax uses identity projection): |
| 44 | +filters.For<Child>().Add( |
| 45 | + filter: (_, _, _, c) => c.Parent.Property == "test"); |
| 46 | +``` |
| 47 | + |
| 48 | +### Examples of correct code |
| 49 | + |
| 50 | +```csharp |
| 51 | +// ✅ Explicit projection extracting required properties |
| 52 | +filters.For<Child>().Add( |
| 53 | + projection: c => new { c.Id, ParentProperty = c.Parent.Property }, |
| 54 | + filter: (_, _, _, proj) => proj.ParentProperty == "test"); |
| 55 | + |
| 56 | +// ✅ Concrete (non-abstract) navigation is fine with identity projection |
| 57 | +public class ConcreteParent // Not abstract |
| 58 | +{ |
| 59 | + public string Property { get; set; } |
| 60 | +} |
| 61 | + |
| 62 | +public class Child |
| 63 | +{ |
| 64 | + public ConcreteParent Parent { get; set; } |
| 65 | +} |
| 66 | + |
| 67 | +filters.For<Child>().Add( |
| 68 | + projection: _ => _, |
| 69 | + filter: (_, _, _, c) => c.Parent.Property == "test"); // OK - ConcreteParent is not abstract |
| 70 | +``` |
| 71 | + |
| 72 | +## Solution |
| 73 | + |
| 74 | +Convert the identity projection to an explicit projection that extracts only the required properties from the abstract navigation. |
| 75 | + |
| 76 | +### Manual Fix |
| 77 | + |
| 78 | +1. Identify all properties accessed through the abstract navigation |
| 79 | +2. Create an explicit projection that extracts those properties with flattened names |
| 80 | +3. Update the filter to use the projected property names |
| 81 | +4. Rename the filter parameter to `proj` for clarity |
| 82 | + |
| 83 | +**Before:** |
| 84 | +```csharp |
| 85 | +filters.For<Child>().Add( |
| 86 | + projection: _ => _, |
| 87 | + filter: (_, _, _, c) => c.Parent.Property == "test"); |
| 88 | +``` |
| 89 | + |
| 90 | +**After:** |
| 91 | +```csharp |
| 92 | +filters.For<Child>().Add( |
| 93 | + projection: c => new { c.Id, ParentProperty = c.Parent.Property }, |
| 94 | + filter: (_, _, _, proj) => proj.ParentProperty == "test"); |
| 95 | +``` |
| 96 | + |
| 97 | +### Automatic Fix |
| 98 | + |
| 99 | +The code fixer can automatically perform this transformation: |
| 100 | +1. Place cursor on the diagnostic |
| 101 | +2. Press `Ctrl+.` (or `Cmd+.` on Mac) |
| 102 | +3. Select "Convert to explicit projection" |
| 103 | + |
| 104 | +The fixer will: |
| 105 | +- Extract all accessed navigation properties |
| 106 | +- Create an anonymous type projection with flattened property names |
| 107 | +- Update the filter lambda to use the new property names |
| 108 | +- Rename the filter parameter from entity name to `proj` |
| 109 | + |
| 110 | +## Performance Impact |
| 111 | + |
| 112 | +Using explicit projections instead of identity projections with abstract navigations can significantly improve performance: |
| 113 | + |
| 114 | +**Identity Projection (Inefficient):** |
| 115 | +```sql |
| 116 | +-- Loads all 34 columns from BaseRequest table |
| 117 | +SELECT a.*, br.* |
| 118 | +FROM Accommodation a |
| 119 | +LEFT JOIN BaseRequest br ON a.TravelRequestId = br.Id |
| 120 | +``` |
| 121 | + |
| 122 | +**Explicit Projection (Efficient):** |
| 123 | +```sql |
| 124 | +-- Loads only required columns |
| 125 | +SELECT a.Id, br.GroupOwnerId, br.HighestStatusAchieved |
| 126 | +FROM Accommodation a |
| 127 | +LEFT JOIN BaseRequest br ON a.TravelRequestId = br.Id |
| 128 | +``` |
| 129 | + |
| 130 | +## When to Suppress |
| 131 | + |
| 132 | +**Never.** This diagnostic indicates a real performance issue that should be fixed. There is no valid scenario where loading all columns from an abstract navigation is preferred over loading only the required columns. |
| 133 | + |
| 134 | +## Related Rules |
| 135 | + |
| 136 | +- **GQLEF004** - Suggests using 4-parameter filter syntax for identity projections with key-only access |
| 137 | +- **GQLEF005** - Prevents accessing non-key properties with 4-parameter filter syntax |
| 138 | +- **GQLEF006** - Prevents accessing non-key properties with explicit identity projection |
| 139 | + |
| 140 | +## See Also |
| 141 | + |
| 142 | +- [Filter Projections Documentation](/docs/filters.md#abstract-type-navigations) |
| 143 | +- [Understanding EF Core Projections](https://learn.microsoft.com/en-us/ef/core/querying/how-query-works) |
0 commit comments