Any ideas how to re-write this query to work with Oracle?

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
Posts: 17
Joined: Fri 09 Nov 2012 17:52

Any ideas how to re-write this query to work with Oracle?

Post by cResults » Wed 02 Jan 2013 18:40

Given the schema below, I'm trying to build an EF query that returns Contacts that are missing required Forms. Each Contact has a ContactType that is related to a collection of FormTypes. Every Contact is required to have at lease one Form (in ContactForm) of the FormTypes related to its ContactType.

The query that EF generates from the linq query below works against Sql Server, but not against Oracle.

Code: Select all

var query = ctx.Contacts.Where (c => c.ContactType.FormTypes.Select (ft => ft.FormTypeID).Except(c => c.Forms.Select(f => f.FormTypeID)).Any());
I'm in the process of refactoring a data layer so that all of the EF queries that work against Sql Server will also work against Oracle using Devart's dotConnect data provider.

The error that Oracle is throwing is ORA-00904: "Extent1"."ContactID": invalid identifier.

The problem is that Oracle apparently doesn't support referencing a table column from a query in a nested subquery of level 2 and deeper. The line that Oracle throws on is in the Except (or minus) sub query that is referencing "Extent1"."ContactID". "Extent1" is the alias for Contact that is defined at the top level of the query. Here is Devart's explanation of the Oracle limitation

The way that I've resolved this issue for many queries is by re-writing them to move relationships between tables out of the Where() predicate into the main body of the query using SelectMany() and in some cases Join(). This tends to flatten the query being sent to the database server and minimizes or eliminates the sub queries produced by EF. Here is a similar issue solved using a left outer join ... -using-ef5.

Any ideas on how to re-write this query will be much appreciated. The objective is a query that returns Contacts missing Forms of a FormType required by the Contact's ContactType that works against Oracle and Sql Server.

Schema image referred to above can be see here ... ing-ef-5-0

Site Admin
Posts: 8245
Joined: Thu 14 Aug 2008 12:44

Re: Any ideas how to re-write this query to work with Oracle?

Post by Shalex » Thu 03 Jan 2013 15:26

At first sight we don't see the way this query should be refactored.

Otherwise, you can switch to the native SQL if re-writing query can not be done in a particular case. E.g. (for DbContext):

Code: Select all

ctx.Database.SqlQuery<Contact>("select ...");

Post Reply