Calling 'Read' when the data reader is closed is not a valid operation error using Entity Framework database first approach

asp.net-mvc-5 asp.net-web-api asp.net-web-api2 entity-framework-6

Question

With the use of stored procedures and Entity Framework's database-first approach, I'm building a Web API that will get data from a table.ListAllTeams_Result is an Entity Framework complex type object. I'm iterating through the import function.GetAllTeams() and filling up the difficult kind. When attempting to reach the data access layer, I encounter a problem in my business layer.

I am experiencing the following error code.

var team = _teamRepository.GetAllTeams();

The result of the query cannot be enumerated more than once.

Note that even if this problem occurs in the inner stack, the programme continues to run.

foreach (var t in team)

Calling 'Read' when the data reader is closed is not a valid operation.

Nota: This halts operation.

Commercial Layer

public IEnumerable<TeamDto> GetTeam()
{
   var team = _teamRepository.GetAllTeams();

   if (team != null)
   {
       foreach (var t in team.ToList())
       {
           yield return Mapper.Map<TeamDto>(t);
       }
   }

   yield break;
}

Layer for Data Access:

public IEnumerable<ListAllTeams_Result> GetAllTeams()
{
    using (var mcrContext = new MCREntities())
    {
        return (from team in mcrContext.ListAllTeams("")
                select new ListAllTeams_Result
                        {
                            TeamID = team.TeamID,
                            TeamDescription = team.TeamDescription,
                            CountryCode = team.CountryCode,
                            CreatedBy = team.CreatedBy,
                            CreatedDate = team.CreatedDate,
                            ModifiedBy = team.ModifiedBy,
                            ModifiedDate = team.ModifiedDate
                        });
    }
}
1
0
8/5/2017 10:43:00 AM

Popular Answer

ZZZ_tmp
1
3/13/2017 2:12:53 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow