我在EF 4.0上运行联接linq查询时遇到问题。下面是代码,去除了所有不必要的报表,还是再现错误)使用Linq与实体框架4.0一起失败
using (var threadRepo = new Repo<Thread>())
using (var postRepo = new Repo<Post>())
{
var query = threadRepo
.Join(postRepo, t => t.PostId, s => s.Id, (t, s) => 1);
var array = query.ToArray();
}
回购是我实现仓库的模式,以及加入方法是这样的:
public IEnumerable<TResult> Join<TInner, TKey, TResult>(
IEnumerable<TInner> inner,
Expression<Func<TEntity, TKey>> outerSelector,
Expression<Func<TInner, TKey>> innerSelector,
Expression<Func<TEntity, TInner, TResult>> result)
{
return _objectSet.Join(inner, outerSelector, innerSelector, result);
}
错误我得到的是
Unable to create a constant value of type 'Post'.
Only primitive types ('such as Int32, String, and Guid')
are supported in this context.
同样的查询在LinqPad工作针对同一数据库(虽然offcourse,有没有EF 4.0有)
from t in Thread
join p in Post on t.PostId equals p.Id
select 1
Linq给我这个例外的任何线索?
更新
基于下面的建议,我尝试使用一个共同的DataContext两个使用工作单元的存储库。但是,这似乎并没有解决这个问题。下面是我使用的代码
using (var uow = new UnitOfWork<CommunicationEntities>())
{
using (var threadRepo = new Repo<Thread>(uow))
using (var postRepo = new Repo<Post>(uow))
{
var query = threadRepo
.Join(postRepo, t => t.PostId, s => s.Id, (t, s) => 1);
var array = query.ToArray();
}
}
这给了我和以前一样的错误。
感谢 Jaspreet
'sparkRepo'与'postRepo'是否一样? –
是的,对不起。我纠正了错字。 – floatingfrisbee