1
我在沒有任何T4模板生成的情況下使用EF 4中的POCO對象。編譯查詢只允許標量參數!
我有一個DataContext類,它將所有ObjectSets,像這樣
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()
);
如果我修改查詢,這樣它的工作原理,但我深深懷疑其被編譯每一次,因爲我沒有看到性能提升,我會從編譯的查詢看到,可以有人指出最新消息在?上?
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
);