Search code examples
entity-frameworknpgsqlentity-framework-6.1

Npgsql - EF6 : Nested FirstOrDefault() on navigation property subquery throws "Not Implemented" Exception


Structure is basic. I have a "App" Parent which has Many "PositionData" children. I want to retrieve some basic data for the "App" Along with the Last "PositionData" of that App in a single query.

The Query

            var data = context.Apps.Where(a => a.Id == appId).
            Select(c => new {
                DeviceInfo=c.Device,
                LastPosition=c.PositionData.OrderByDescending(p=>p.DateCreated).FirstOrDefault() 
            }).SingleOrDefault();

Executing the following command throws "System.NotImplementedException"

To make sure that exception is thrown only in case of a subquery, i broke this into 2 queries and it works perfectly fine.

            var tempObj = context.Apps.Where(a => a.Id == appId).SingleOrDefault();
            var data=new {
                DeviceInfo=tempObj.Device,
                LastPosition=tempObj.PositionData.OrderByDescending(p=>p.DateCreated)).FirstOrDefault() 
            };

I have been searching for many days,also visited pg foundry forums but no solution yet.


Solution

  • I have Opened the Issue at the Npgsql Git Hub Repository. And it seemed that this error was caused indeed by a not Implemented Npgsql Feature.

    They have now implemented the feature and merged it to the master branch. Downloading Npgsql and NpgsqlEntityFramework .dll versions >= 2.2 will fix the issue . ALTHOUGH this fix uses the "Lateral" sql keyword which was introduced in PostgreSQL v9.3 so that Postgresql Version is also needed.