简体   繁体   中英

Entity Framework 4.3 and Threading

I am getting the error below when running a query in Entity Framework 4.3 in a thread.

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

Below is where my thread starts and it errors at var item = _gamesRepository.Get(gameIncludes, q => q.Id == gameId); . Am I doing something wrong or should I use a different approach?

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();
        }

UPDATE :

I made the required changes to fix that issue.

var gamesRepository = DependencyResolver.Current.GetService<IGamesRepository>();
var divisionBracketsService = DependencyResolver.Current.GetService<IDivisionBracketsService>();

Repository and unit of work should be owned by your thread for two reasons:

  • EF is not thread safe - sharing context among threads can lead you to false belief that you can do concurrent operations on the same context instance but you in general can't. This may change in EF6 where async support will be implemented but current version is only for single threaded processing.
  • If you share context between threads you must ensure that the thread owning the context does not dispose the context before any other thread dependent on that context finishes its processing - that is your current problem. It means that your calling thread should wait on your processing thread to complete.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM