Cannot Bind Multi-part identifier

Posts   
 
    
Posts: 15
Joined: 13-Mar-2011
# Posted on: 15-Mar-2011 07:34:54   

Hi,

I am using the AdventureWorks database to learn the llblgen api. I wanted to bring back a result set that contained only products which are bikes. To that end, I wrote the following code:


        public EntityCollection<ProductEntity> GetBikes()
        {
            EntityCollection<ProductEntity> products = new EntityCollection<ProductEntity>();
            RelationPredicateBucket bucket = new RelationPredicateBucket();
            bucket.PredicateExpression.Add(ProductCategoryFields.ProductCategoryId == 1);

            using (DataAccessAdapter productsAdapter = DataAccess.AdaptorFactory.GetNewAdaptor(true))
            {
                productsAdapter.FetchEntityCollection(products, bucket);
            }

            return products;
        }

The following exception was thrown when that method was invoked:

The multi-part identifier "AdventureWorks.Production.ProductCategory.ProductCategoryID" could not be bound.

Can anyone let me know how I can rectify that? Thanks

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 15-Mar-2011 09:42:43   

Please try the following:

            EntityCollection<ProductEntity> products = new EntityCollection<ProductEntity>();
            RelationPredicateBucket bucket = new RelationPredicateBucket();
            bucket.PredicateExpression.Add(ProductFields.ProductCategoryId == 1);

            using (DataAccessAdapter productsAdapter = DataAccess.AdaptorFactory.GetNewAdaptor(true))
            {
                productsAdapter.FetchEntityCollection(products, bucket);
            }

            return products;

The issue is: you have filtered on a field in a related entity, without specifying the rlation to it (Join).

But anyway I think in your case you don't need the ProductCategory Entity. As you have the CategoryId in the Product Entity.

But for the sake of demonstrating here how it would look like:

            EntityCollection<ProductEntity> products = new EntityCollection<ProductEntity>();
            RelationPredicateBucket bucket = new RelationPredicateBucket();
            bucket.PredicateExpression.Add(ProductCategoryFields.ProductCategoryId == 1);
            bucket.Relations(ProductEnity.Relations.ProductCategoryEntity);

            using (DataAccessAdapter productsAdapter = DataAccess.AdaptorFactory.GetNewAdaptor(true))
            {
                productsAdapter.FetchEntityCollection(products, bucket);
            }

            return products;
Posts: 15
Joined: 13-Mar-2011
# Posted on: 16-Mar-2011 01:22:33   

Thanks Walaa,

Your code worked well (both snippets). Your explanation made perfect sense.

Much appreciated!