错误LINQ表达式包含对与不同上下文关联的查询的引用

时间:2013-04-02 11:29:13

标签: linq join entity-framework-5

我在执行以下查询时遇到错误

错误: 指定的LINQ表达式包含对与不同上下文关联的查询的引用

查询:

var wordlePuzzle = (from wp in wordlepostitionRepository.All
join wtc in wordletextclueRepository.All on wp.WordleTexCluetId equals wtc.WordleTextClueId
join w in wordleRepository.All on wtc.WordleId equals w.WordleId
where wtc.WordleId == id
select new WordlePuzzle
{
   Row = w.WordleRow,
   Column = w.WordleColumn,
   Title = w.WordleTitle,
   DifficultyLevel = DifficultyLevel.Easy,
   Duration = w.WordleDuration,
   ExpiresOn = w.ExpireDate
});

我尝试在查询结束后附加.AsEnumerable();,但仍然发出错误。

我所有这些表只有一个数据库。

1 个答案:

答案 0 :(得分:2)

查看实现工作单元模式,以便您可以跨存储库共享上下文实例。

网络上有很多例子,例如: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application