Npgsql Error - The parameter already belongs to a collection

Posts   
 
    
usschad
User
Posts: 71
Joined: 11-Sep-2008
# Posted on: 04-Oct-2017 20:15:54   

LLBLGen 3.5 Final (January 17th, 2013) 3.5.13.1015 Adapter Template Postgres 9.2 Npgsql 2.1.3.0 .NET 4.5.1

I read that Frans had resolved an issue with this particular error in the Npgsql forum I found on google: https://groups.google.com/forum/#!topic/npgsql-dev/CLOKpjHci5M

I looked at the npgsql release info, and it looks like Frans' fix didn't get in until 2.2. Should I be safe using that? Or am I risking a lot of breaking changes?

I'm assuming that I may need to just upgrade my npgsql? What npgsql version would you recommend that I upgrade without breaking something else?

But just for good measure, here is the code:


        public EntityCollection<TrxBatchEntity> FetchBatches(DateTime? statementDate, BatchType batchType, string currencyCode)
        {

            EntityCollection<TrxBatchEntity> batches = new EntityCollection<TrxBatchEntity>();
            IRelationPredicateBucket filter = new RelationPredicateBucket();

            if (!statementDate.HasValue && batchType == BatchType.Unspecified && string.IsNullOrEmpty(currencyCode))
                //exclude balanced and deleted batches from this query if no filter criteria was specified.
                filter.PredicateExpression.Add(BitFilter(TrxBatchFields.Flags, 1, true) & BitFilter(TrxBatchFields.Flags, 2, true));
            else
            {
                if (statementDate.HasValue)
                    filter.PredicateExpression.Add(TrxBatchFields.StatementDate == statementDate);
                if (batchType != BatchType.Unspecified)
                    filter.PredicateExpression.Add(TrxBatchFields.Batchtype == (int)batchType);
                if (!string.IsNullOrEmpty(currencyCode))
                    filter.PredicateExpression.Add(TrxBatchFields.CurrencyCode == currencyCode);
            }
                


            Adapter.FetchEntityCollection(batches, filter, 0, 
                new SortExpression(TrxBatchFields.StatementDate | SortOperator.Ascending), 
                GetTrxBatchPrefetches());


            return batches;

        }

        private PrefetchPath2 GetTrxBatchPrefetches()
        {
            //TrxAccount records without a BankAccount record, and a BankAccount record without a BankAccountOwner record is a problem. 
            //We want to bring in BankAccount and BankAccountOwner records even with deleted and inactive flags so that we can display the data to the UI and present the error messages accordingly.

            PrefetchPath2 prefetch = new PrefetchPath2(EntityType.TrxBatchEntity);
            IPrefetchPathElement2 trxAccountPrefetch = prefetch.Add(TrxBatchEntity.PrefetchPathTrxMemberCollection).SubPath.Add(TrxMemberEntity.PrefetchPathTrxAccountCollection);
            trxAccountPrefetch.Filter.Add(BitFilter(TrxAccountFields.Flags, 2, true)); //exclude deleted trxaccounts
            trxAccountPrefetch.Sorter.Add(new SortClause(TrxAccountFields.TrxAccountId, null, SortOperator.Ascending));
            trxAccountPrefetch.SubPath.Add(TrxAccountEntity.PrefetchPathTrxAccountPaymentCollection)
                .Sorter.Add(new SortClause(TrxAccountPaymentFields.EffectiveDate, null, SortOperator.Ascending));
            IPrefetchPathElement2 trxBatchFilePrefetch = prefetch.Add(TrxBatchEntity.PrefetchPathTrxBatchFileCollection);
            trxBatchFilePrefetch.Sorter.Add(new SortClause(TrxBatchFileFields.Id, null, SortOperator.Ascending));
            IPrefetchPathElement2 imagePrefetch = trxBatchFilePrefetch.SubPath.Add(TrxBatchFileEntity.PrefetchPathImage);
            imagePrefetch.ExcludedIncludedFields = new ExcludeIncludeFieldsList(true);
            imagePrefetch.ExcludedIncludedFields.Add(ImageFields.Bytes); //don't pull in file data 


            IPrefetchPathElement2 bankAccountPrefetch = trxAccountPrefetch.SubPath.Add(TrxAccountEntity.PrefetchPathBankAccount);
            bankAccountPrefetch.SubPath.Add(BankAccountEntity.PrefetchPathBankAccountOwner);
            return prefetch;
        }


Stack Trace:

at Npgsql.NpgsqlParameterCollection.Add(NpgsqlParameter value) in c:\dev\TeamCity\buildAgent\work\c548594815738d27\Npgsql\Npgsql\NpgsqlParameterCollection.cs:line 203 at Npgsql.NpgsqlParameterCollection.Add(Object value) in c:\dev\TeamCity\buildAgent\work\c548594815738d27\Npgsql\Npgsql\NpgsqlParameterCollection.cs:line 638 at SD.LLBLGen.Pro.ORMSupportClasses.Query.AddParameters(List`1 parametersToAdd) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.5\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\Query\Query.cs:line 243 at SD.LLBLGen.Pro.ORMSupportClasses.DynamicQueryEngineBase.CreateSelectDQ(IEntityFieldCore[] selectList, IFieldPersistenceInfo[] fieldsPersistenceInfo, IRetrievalQuery query, IPredicate selectFilter, Int64 maxNumberOfItemsToReturn, ISortExpression sortClauses, IRelationCollection relationsToWalk, Boolean allowDuplicates, IGroupByCollection groupByClause, Boolean relationsSpecified, Boolean sortClausesSpecified) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.5\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\Persistence\DynamicQueryEngineBase.cs:line 1273 at SD.LLBLGen.Pro.ORMSupportClasses.DynamicQueryEngineBase.CreateSelectDQ(IEntityFieldCore[] selectList, IFieldPersistenceInfo[] fieldsPersistenceInfo, DbConnection connectionToUse, IPredicate selectFilter, Int64 maxNumberOfItemsToReturn, ISortExpression sortClauses, IRelationCollection relationsToWalk, Boolean allowDuplicates, IGroupByCollection groupByClause) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.5\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\Persistence\DynamicQueryEngineBase.cs:line 1137 at SD.LLBLGen.Pro.DQE.PostgreSql.DynamicQueryEngine.CreatePagingSelectDQ(IEntityFieldCore[] selectList, IFieldPersistenceInfo[] fieldsPersistenceInfo, DbConnection connectionToUse, IPredicate selectFilter, Int64 maxNumberOfItemsToReturn, ISortExpression sortClauses, IRelationCollection relationsToWalk, Boolean allowDuplicates, IGroupByCollection groupByClause, Int32 pageNumber, Int32 pageSize) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.5\Frameworks\LLBLGen Pro\RuntimeLibraries\PostgreSqlDQE\DynamicQueryEngine.cs:line 265 at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.CreateSelectDQ(IEntityFields2 fieldsToFetch, IFieldPersistenceInfo[] persistenceInfoObjects, IPredicateExpression filter, Int64 maxNumberOfItemsToReturn, ISortExpression sortClauses, IRelationCollection relationsToWalk, Boolean allowDuplicates, IGroupByCollection groupByClause, Int32 pageNumber, Int32 pageSize) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.5\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\AdapterSpecific\DataAccessAdapterBase.cs:line 4396 at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchEntityCollectionInternal(IEntityCollection2 collectionToFill, IRelationPredicateBucket& filterBucket, Int32 maxNumberOfItemsToReturn, ISortExpression sortClauses, ExcludeIncludeFieldsList excludedIncludedFields, Int32 pageNumber, Int32 pageSize) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.5\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\AdapterSpecific\DataAccessAdapterBase.cs:line 4519 at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchPrefetchPath(IEntityCollection2 rootEntities, IRelationPredicateBucket filterBucket, Int64 maxNumberOfItemsToReturn, ISortExpression sortClauses, IPrefetchPath2 prefetchPath, Boolean forceParameterizedPPath) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.5\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\AdapterSpecific\DataAccessAdapterBase.cs:line 5137 at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchPrefetchPath(IEntityCollection2 rootEntities, IRelationPredicateBucket filterBucket, Int64 maxNumberOfItemsToReturn, ISortExpression sortClauses, IPrefetchPath2 prefetchPath) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.5\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\AdapterSpecific\DataAccessAdapterBase.cs:line 5005 at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchPrefetchPath(IEntityCollection2 rootEntities, IRelationPredicateBucket filterBucket, Int64 maxNumberOfItemsToReturn, ISortExpression sortClauses, IPrefetchPath2 prefetchPath, Boolean forceParameterizedPPath) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.5\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\AdapterSpecific\DataAccessAdapterBase.cs:line 5148 at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchEntityCollection(IEntityCollection2 collectionToFill, IRelationPredicateBucket filterBucket, Int32 maxNumberOfItemsToReturn, ISortExpression sortClauses, IPrefetchPath2 prefetchPath, ExcludeIncludeFieldsList excludedIncludedFields, Int32 pageNumber, Int32 pageSize) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.5\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\AdapterSpecific\DataAccessAdapterBase.cs:line 2426 at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchEntityCollection(IEntityCollection2 collectionToFill, IRelationPredicateBucket filterBucket, Int32 maxNumberOfItemsToReturn, ISortExpression sortClauses, IPrefetchPath2 prefetchPath) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.5\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\AdapterSpecific\DataAccessAdapterBase.cs:line 2317

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 04-Oct-2017 21:45:12   

You really should upgrade to the latest npgsql v3 version, as it's much better than any v2 version. v3 does come with its share of breaking changes but then again, most are in features you likely didn't use or didn't work at all anyway wink

Frans Bouma | Lead developer LLBLGen Pro