实体框架4.3和线程

时间:2012-08-16 18:22:26

标签: multithreading entity-framework entity-framework-4 entity-framework-4.1

我在线程中的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>();

1 个答案:

答案 0 :(得分:3)

存储库和工作单元应由您的线程拥有,原因有两个:

  • EF不是线程安全的 - 线程之间的共享上下文可能会导致您错误地认为您可以在同一个上下文实例上执行并发操作但通常不能。这可能会在EF6中发生变化,其中将实现async支持但当前版本仅用于单线程处理。
  • 如果您在线程之间共享上下文,则必须确保拥有上下文的线程在依赖于该上下文的任何其他线程完成其处理之前不会释放上下文 - 这是您当前的问题。这意味着您的调用线程应该等待您的处理线程完成。