以下查询在EF5上运行良好,但在EF6上给出了错误。我使用DbContext和CodeFirst。
使用EF6引发错误:
无法转换类型'System.Linq.IQueryable
1[[<>f__AnonymousType5
4 [[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]]'输入'System.Data.Entity.Core.Objects.ObjectQuery1[[<>f__AnonymousType5
4 [[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 = 85 d69d39f5becc93]]”。 LINQ to Entities仅支持转换EDM原语或枚举类型。
查询(简化):
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();
问题似乎出现在左边连接中,并在变量'orders'中加载了匿名类型。我还尝试在'orders'查询中添加ToList(),然后将错误更改为:
无法创建“匿名类型”类型的常量值。在此上下文中仅支持原始类型或枚举类型。
有人能给我一个如何解决它的提示吗?
更新:该错误已在EF 6.0.2中得到确认和解决。 See the Issue on CodePlex.
答案 0 :(得分:1)
正如dna2在他的问题末尾所指出的那样,这个问题已在EF 6.0.2
中修复。要升级您的EF版本,请在程序包管理器控制台(VS2013 VIEW -> Other Windows -> Package Manager Console
)中执行以下操作。
Update-Package EntityFramework -Version 6.0.2