Skip to content

InvalidOperationException on fetching associations in LINQPAD results view #7

@henrik-m

Description

@henrik-m

When I issue the following LINQPAD query

entities.Take (100)

all entities with a one-to-many or many-to-one association to the returned objects fail to load with an InvalidOperationException. This is the StackTrace:

at Npgsql.NpgsqlConnector.StartUserAction(ConnectorState newState)
at Npgsql.NpgsqlCommand.ExecuteDbDataReaderInternal(CommandBehavior behavior)
at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
at LinqToDB.Data.DataConnection.ExecuteReader(CommandBehavior commandBehavior)
at LinqToDB.Data.DataConnection.LinqToDB.IDataContext.ExecuteReader(Object query)
at LinqToDB.Linq.Query1.d__11.MoveNext()atLinqToDB.Linq.Query1.<Map>d__6a.MoveNext()
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable1 source, Func2 predicate)

The reason for this is that Linq2Db tries to open a DataReader on a connection where there already is an open DataReader. I guess LINQPAD executes something like this to fetch the results and their associations:

using (var dataContext = new TypedDataContext("PostgreSQL", "connectionString"))
{
    foreach(var entity in dataContext.entities.Take(100))
    {
        var associatedEntity = entity.manyToOneEntity;
    }
}

This fails with the same error because Entity.ResolveOneToMany and Entity.ResolveManyToOne are called in the getter of the association.

A workaround for this is to change the query to entities.Take (100).ToList() to close the DataReader before the associations are fetched, but this obviously only fixes the 1st level of association fetching in the results view. One layer down associations still greet us with an InvalidOperationException.

I'm not sure how best to fix this... I guess there would have to be a separate DataConnection for each level of association fetching.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions