4
votes

The following query was working well with EF5 but gives me error with EF6. I use DbContext and CodeFirst.

Error raised with EF6:

Unable to cast the type 'System.Linq.IQueryable1[[<>f__AnonymousType54[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], Wms.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=85d69d39f5becc93]]' to type 'System.Data.Entity.Core.Objects.ObjectQuery1[[<>f__AnonymousType54[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], Wms.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=85d69d39f5becc93]]'. LINQ to Entities only supports casting EDM primitive or enumeration types.

Query (simplified):

var unitsPerPosition = context.Positions.Include(p => p.Unit).Select(x => new { Unit = x.Unit, Position = x });

var orders = (
                from order in context.Orders.Include(x => x.SourceLocation);
                join unitPosition in unitsPerPosition on order.UnitId equals unitPosition.Unit.UnitId
                group new { order, unitPosition } by new { LocationId = order.SourceLocationId, Level = unitPosition.Position.Level } into groupOrders
                where groupOrders.Key.LocationId != null
                select new { 
                    LocationId = groupOrders.Key.LocationId.Value, 
                    Level = groupOrders.Key.Level, 
                    LoadingOrders = groupOrders.Count(), 
                    UnloadingOrders = -1 }
            );

IList<LocationChannels> searchResult = (from s in context.Locations
        .Include(s => s.Positions)
        .Include(s => s.Positions.Select(sp => sp.Unit))

        let channels = from p in s.Positions
                        where p.LocationId == s.LocationId
                        group p by new { p.LocationId, p.Column, p.Level } into channelsGroup

                        join order in orders on new { channelsGroup.Key.LocationId, channelsGroup.Key.Level } 
                        equals new { order.LocationId, order.Level } into ordersGroup
                        from o in ordersGroup.DefaultIfEmpty()

                        select new Channel
                        {
                            LocationId = channelsGroup.Key.LocationId,
                            Column = channelsGroup.Key.Column,
                            Level = channelsGroup.Key.Level
                        }

        select new LocationChannels
                      {
                        Location = s,
                        Channels = channels,
                      })
    .Where(predicate).ToList();

The issue appears to be on the left join with the anonymous type loaded in variable 'orders'. I also tried to add a ToList() to the 'orders' query but then the error change to:

Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context.

Can someone gives me an hint on how to fix it?

UPDATE: The bug has been confirmed and resolved in EF 6.0.2. See the Issue on CodePlex.

1
Check the type of StorageLocationId and Level from each of those object. Chances are one is nullable and another isn't, or one is a long and another an int or something along those lines.Servy
I'm not in front of the PC right now but I'm quite sure all values are integers. Also the exception shows the two types (IQuerable and ObjectQuery) have four int each. In the first query (tos) I had a nullable int but I used the .Value. I'll double check tomorrow morning first. ThxDan
Try removing UnloadingOrders = -1AaronLS
Do you mind filing a bug on entityframework.codeplex.com? Please provide stack trace and the model if possible. This seems like a bug in EF6.Pawel
Posted here: entityframework.codeplex.com/workitem/1751 Asap I'll edit with stack trace and model.Dan

1 Answers

1
votes

As noted by dna2 at the end of his question, this issue was fixed in EF 6.0.2. To upgrade your version of EF, do the following in the Package Manager Console (VS2013 VIEW -> Other Windows -> Package Manager Console).

Update-Package EntityFramework -Version 6.0.2