Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does EF 5.0 not support this EF 4.x LINQ syntax when compiling to sql?
    primarykey
    data
    text
    <p>I have some code that was recently upgraded from EF 4.2 to EF 5.0 (actually EF 4.4 since I am running on .Net 4.0). I have discovered that I had to change the syntax of my query, and I'm curious as to why. Let me start off with the problem.</p> <p>I have an EventLog table that is populated by the client periodically. For each event log an entry is created in a Report table. This is the query that is run periodically to discover any event logs that do not have an entry in the Report table yet. The query I used in EF 4.2 was:</p> <pre><code>from el in _repository.EventLogs where !_repository.Reports.Any(p =&gt; p.EventLogID == el.EventlogID) </code></pre> <p>Since upgrading to EF 5.0 I get the following error at runtime:</p> <blockquote> <p>System.NotSupportedException: Unable to create a constant value of type 'Namespace.Report'. Only primitive types or enumeration types are supported in this context.</p> </blockquote> <p>I discovered that rewriting it with the join syntax fixed the issue. The following works in EF 5.0 and is roughly the equivalent:</p> <pre><code>from eventLog in _repository.EventLogs join report in _repository.Reports on eventLog.EventlogID equals report.EventLogID into alreadyReported where !alreadyReported.Any() </code></pre> <p>Some people may have mixed opinions about the mixed syntax/style of the first query, but I'm really more interested in the why of this. It seems odd that the EF 4.2 compiler could generate the SQL for the original query but that the EF 5.0 refuses. Is this a setting I am missing or just a tightening of constraints between the two? Why is this happening?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload