在线程中的 Entity Framework 4.3 中运行查询时出现以下错误。
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
下面是我的线程开始的地方,它在var item = _gamesRepository.Get(gameIncludes, q => q.Id == gameId);
. 我做错了什么还是应该使用不同的方法?
public void ProcessGame(int gameId)
{
new Thread(() =>
{
Expression<Func<Game, object>>[] gameIncludes = {
q => q.DivisionGameTeamResults,
q => q.DivisionGameTeamResults.Select(g => g.DivisionBracketGameParticipant),
q => q.DivisionGameTeamResults.Select(g => g.DivisionTeamPoolGame),
q => q.DivisionGameTeamResults.Select(g => g.DivisionTeamPoolGame.DivisionTeamPool),
e => e.DivisionGameTeamResults.Select(q => q.DivisionBracketGameParticipant.DivisionBracketGame.DivisionBracketGameParticipants.Select(t => t.DivisionBracketGameParticipantTeam.DivisionTeam.Team)),
e => e.DivisionGameTeamResults.Select(q => q.DivisionBracketGameParticipant.DivisionBracketGame.DivisionLoserBracketGameParticipants.Select(d => d.DivisionBracketGameParticipantPool.DivisionPool)),
e => e.DivisionGameTeamResults.Select(q => q.DivisionBracketGameParticipant.DivisionBracketGame.DivisionLoserBracketGameParticipants.Select(d => d.DivisionBracketGameParticipantTeamPool.DivisionTeamPool.DivisionTeam)),
q => q.DivisionGameTeamResults.Select(d => d.DivisionTeamPoolGame.DivisionTeamPool.DivisionPool.Division.Event.Members),
q => q.DivisionGameTeamResults.Select(d => d.DivisionBracketGameParticipant.DivisionBracketGame.BracketPart.DivisionBracketPart.DivisionBracket.Division.Event.Members)
};
var item = _gamesRepository.Get(gameIncludes, q => q.Id == gameId);
if (item != null)
{
if (item.DivisionGameTeamResults.All(d => d.DivisionTeamPoolGame != null))
{
// Pool Game
_divisionBracketsService.ProcessPoolGame(item);
}
else if (item.DivisionGameTeamResults.All(d => d.DivisionBracketGameParticipant != null))
{
// Bracket Game
_divisionBracketsService.ProcessBracketGame(item);
}
UnitOfWork.Commit();
}
}).Start();
}
更新:
我进行了必要的更改来解决该问题。
var gamesRepository = DependencyResolver.Current.GetService<IGamesRepository>();
var divisionBracketsService = DependencyResolver.Current.GetService<IDivisionBracketsService>();