Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NpGsql EntityFramework 6 - "An operation is already in progress"

I am working on a project to connect to PostgreSQL database using NpGsql EntityFramework 6. I am getting the exception in question heading, when I try to execute the query in GetAdminUsersCount:

public class GenieRepository : IDisposable
{
    GenieDbContext db = new GenieDbContext();
    public IEnumerable<User> GetUsers()
    {
        return db.Users;
    }   
}

public int GetAdminUsersCount()
{
    return repo.GetUsers().Where(u => u.Role.RoleName == "Administrator").Count();
}

What is the reason for this error and how to resolve it?

like image 740
teenup Avatar asked Dec 31 '15 09:12

teenup


1 Answers

I have succeeded to solve the problem using a ToList<T> right after the linq query like this:

using (ElisContext db = new ElisContext()) {
    var q = from a in db.aktie select a;
    List<aktie> akties = q.ToList<aktie>();
    foreach (aktie a in akties) {
        Console.WriteLine("aktie: id {0}, name {1}, market name {2}"
                 , a.id, a.name, a.marked.name);
    }
}

Note the q.ToList<T> that does the trick. .Net postpone the execution of the linq statement to the latest moment, which may be part of the problem. I have tried to use q in the foreach with out success.

like image 66
Jan Rou Avatar answered Nov 07 '22 04:11

Jan Rou