I have the following bits of code in my accountRepository
public string[] GetRolesForUser(string email)
{
// User rolesUser = FindByMail(email);
IEnumerable<UserRole> RoleList = context.UserRolesSet.Where(u => u.user_id == 1).AsEnumerable();
string[] arr1 = new string[RoleList.Count()];
int i = 0;
foreach (UserRole r in RoleList)
{
arr1[i] = r.roles.name;
i++;
}
return arr1;
}
This should work but it doesn’t. When it looping through the foreach loop it throws me this error:
Exception Details: MySql.Data.MySqlClient.MySqlException: There is already an open DataReader associated with this Connection which must be closed first.
Is my foreach loop wrong?
Try the following code. Using ToArray makes sure it populates all of the UserRoles before hand so it’ll be finished with the DataReader.