4

I'm creating a web application using C# and the .NET Entity Framework for the back-end. I have three PostgreSQL tables handling my data: events, users, and groups. Users belong to groups, and groups belong to events.

What I am trying to do is allow for an HTTP GET request to get all users for event. (The API call contains the event ID. Ideally, the user could send a GET request to api/events/1/users, which would then:

  1. Search through the groups table for all groups with an eventid of 1;

  2. Get the list of users from each of those groups.

Per this website's instructions, I set up my related data (many-to-many) in the model for users and groups. The problem is that doing this broke some methods that were working previously.

This is my group model:

namespace api.Models
{
    public class Group
    {
        public Group()
        {
            this.users = new HashSet<User>();
        }

        [Key]
        public int id { get; set; }

        public int eventid { get; set; }

        [Required]
        public string groupname { get; set; }

        public virtual ICollection<User> users { get; set; }
    }
}

... And this is my user model:

namespace api.Models
{

    public class User
    {

        public User()
        {
            this.groups = new HashSet<Group>();
        }

        [Key]
        public string id { get; set; }

        [Required]
        public string phone { get; set; }

        public virtual ICollection<Group> groups { get; set; }
    }
}

I'm able to query a single group using the following method in my groups controller:

[Route("groups/{id}",Name="GetGroup")]
[HttpGet]
[ResponseType(typeof(Group))]
public async Task<IHttpActionResult> GetGroup(int id)
{
    Group @group = await db.groups.FindAsync(id);
    if (@group == null)
    {
        return NotFound();
    }

    return Ok(@group);
}

This returns a single group, with a list of all its users, as expected.

The problem is that this method no longer works:

[Route("events/{eventid}/groups")]
[HttpGet]
public IQueryable<Group> GetGroupsByEvent(int eventid)
{
    return db.groups.Where(l => l.eventid == eventid);
}

This worked until I added related users to the groups model. Now when I try to make a request to api/events/1/groups, for instance, I get a Npgsql.NpgsqlOperationInProgressException with the following message:

"ExceptionMessage": "A command is already in progress: SELECT \"Extent1\".\"id\", \"Extent1\".\"eventid\", \"Extent1\".\"groupname\" FROM \"public\".\"Groups\" AS \"Extent1\" WHERE \"Extent1\".\"eventid\" = @p__linq__0",

Here's the debug output from Visual Studio:

Opened connection at 9/26/2017 8:35:32 AM -04:00

SELECT "Extent1"."id", "Extent1"."eventid", "Extent1"."groupname" FROM "public"."Groups" AS "Extent1" WHERE "Extent1"."eventid" = @p__linq__0

-- p__linq__0: '1' (Type = Int32, IsNullable = false)

-- Executing at 9/26/2017 8:35:32 AM -04:00

-- Completed in 1 ms with result: NpgsqlDataReader

SELECT "Extent2"."id", "Extent2"."phone" FROM "public"."UserGroups" AS "Extent1" INNER JOIN "public"."Users" AS "Extent2" ON "Extent1"."User_id" = "Extent2"."id" WHERE "Extent1"."Group_id" = @EntityKeyValue1

-- EntityKeyValue1: '1' (Type = Int32, IsNullable = false)

-- Executing at 9/26/2017 8:35:32 AM -04:00

-- Failed in 0 ms with error: A command is already in progress: SELECT "Extent1"."id", "Extent1"."eventid", "Extent1"."groupname" FROM "public"."Groups" AS "Extent1" WHERE "Extent1"."eventid" = @p__linq__0

Closed connection at 9/26/2017 8:35:32 AM -04:00

I am unable to determine why this is happening, and what in my code could be changed to allow these multiple queries to work as intended.

Thanks for any insight you can give.

Ben Schroeder
  • 346
  • 2
  • 7
  • Could [this](https://stackoverflow.com/questions/39595968/entityframework-dbcontext-lifecycle-postgres-an-operation-is-already-in-prog) be related to your problems? – Michal S Sep 26 '17 at 13:58
  • 2
    Use eager instead of lazy loading. – Ivan Stoev Sep 26 '17 at 17:32
  • In addition of @IvanStoev comment: [Eager explained](https://www.c-sharpcorner.com/article/eager-loading-lazy-loading-and-explicit-loading-in-entity-framework/). – mins May 12 '19 at 13:17

0 Answers0