Entity framework 4 编译查询只允许使用标量参数!
我在EF4中使用POCO对象,没有生成任何T4模板 我有一个DataContext类,它封装了所有对象集,类似这样Entity framework 4 编译查询只允许使用标量参数!,entity-framework-4,Entity Framework 4,我在EF4中使用POCO对象,没有生成任何T4模板 我有一个DataContext类,它封装了所有对象集,类似这样 public sealed class DataContext :IDisposable { public IObjectSet GetObjectSet() where T : MyBase { object objectSet = null; this.objectSets.TryGetValue(typeof(T), out objectSet);
public sealed class DataContext :IDisposable
{
public IObjectSet GetObjectSet() where T : MyBase
{
object objectSet = null;
this.objectSets.TryGetValue(typeof(T), out objectSet);
if (objectSet == null)
{
objectSet = this.context.CreateObjectSet();
this.objectSets.Add(typeof(T), objectSet);
}
return (IObjectSet)objectSet;
}
public ObjectContext ObjectContext
{
get
{
return this.context;
}
}
}
当我编写下面的编译查询并尝试将该类作为参数之一传入时,它会给出一个运行时错误,表示只允许使用标量参数
static readonly Func<ObjectContext , DataContext, string, int?> getOperationByOrchestrationName
= CompiledQuery.Compile(
(ObjectContext ctx, DataContext container, string name) =>
(from or in container.GetObjectSet<MyOrClass>()
join op in container.GetObjectSet<MyOpClass>()
on or.Id equals op.Id
where op.Name == name
select op.Id).FirstOrDefault()
);
对于任何感兴趣的人来说,如果您从编译后的查询返回IQueryable并调用任何可以更改查询的方法(singleordefault或firstordefault等),您将无法从编译后的查询中获益
static readonly Func, IObjectSet, string, IQueryable>
getOperationByOrchestrationName
= CompiledQuery.Compile(
(ObjectContext ctx, IObjectSet ors, IObjectSet ops,string operationName) =>
from or in ors
join op in ops
on or.Id equals op.Id
where op.Name == name
select op.Id
);