Empty collection properties after calling SearchProducts

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
Hi Everyone,

I'm helping a fellow community member debug some code and we've come into an interesting situation. In the Nop.Web.Controllers.CatalogController we're debugging the Category(categoryId, command) method.

Line 851 we make a call to the ProductService.SearchProducts method.


            var products = _productService.SearchProducts(category.Id, 0, false, minPriceConverted, maxPriceConverted,
                0, string.Empty, false, _workContext.WorkingLanguage.Id, selectedSpecs,
                (ProductSortingEnum)command.OrderBy, command.PageNumber - 1, command.PageSize);
            model.Products = products.Select(x => PrepareProductOverviewModel(x)).ToList();


A part of this project the developer needs access to the collection properties on each product like ProductVariants and Manufacturers. When I install the sample data and debug I am able to see these properties being populated. During my testing I forced the ProductService to use the stored procedure once and to use the LINQ query once. Both times it worked correctly however the person I'm working with is not able to see these same results when working with his production data.

Is there any configuration (besides bad data) that would lead to these properties not having the appropriate lists of records after calling SearchProducts?

Thanks!
12 years ago
skyler.severns wrote:
...however the person I'm working with is not able to see these same results when working with his production data...

Is he using stored procedure or LINQ version? If he's using SP, then please post his 'ExecuteStoredProcedureList' method of the Nop.Data.NopObjectContext class here.
12 years ago
a.m. wrote:
...however the person I'm working with is not able to see these same results when working with his production data...
Is he using stored procedure or LINQ version? If he's using SP, then please post his 'ExecuteStoredProcedureList' method of the Nop.Data.NopObjectContext class here.


Yep using SP , Nop 2.2,
public IList<TEntity> ExecuteStoredProcedureList<TEntity>(string commandText, params object[] parameters) where TEntity : BaseEntity, new()
        {
            //HACK: Entity Framework Code First doesn't support doesn't support output parameters
            //That's why we have to manually create command and execute it.
            //just wait until EF Code First starts support them
            //
            //More info: http://weblogs.asp.net/dwahlin/archive/2011/09/23/using-entity-framework-code-first-with-stored-procedures-that-have-output-parameters.aspx

            bool hasOutputParameters = false;
            if (parameters != null)
            {
                foreach (var p in parameters)
                {
                    var outputP = p as DbParameter;
                    if (outputP == null)
                        continue;

                    if (outputP.Direction == ParameterDirection.InputOutput ||
                        outputP.Direction == ParameterDirection.Output)
                        hasOutputParameters = true;
                }
            }



            var context = ((IObjectContextAdapter)(this)).ObjectContext;
            if (!hasOutputParameters)
            {
                //no output parameters
                var result = this.Database.SqlQuery<TEntity>(commandText, parameters).ToList();
                foreach (var entity in result)
                    Set<TEntity>().Attach(entity);
                return result;
                
                //var result = context.ExecuteStoreQuery<TEntity>(commandText, parameters).ToList();
                //foreach (var entity in result)
                //    Set<TEntity>().Attach(entity);
                //return result;
            }
            else
            {

                //var connection = context.Connection;
                var connection = this.Database.Connection;
                //Don't close the connection after command execution


                //open the connection for use
                if (connection.State == ConnectionState.Closed)
                    connection.Open();
                //create a command object
                using (var cmd = connection.CreateCommand())
                {
                    //command to execute
                    cmd.CommandText = commandText;
                    cmd.CommandType = CommandType.StoredProcedure;

                    // move parameters to command object
                    if (parameters != null)
                        foreach (var p in parameters)
                            cmd.Parameters.Add(p);

                    //database call
                    var reader = cmd.ExecuteReader();
                    //return reader.DataReaderToObjectList<TEntity>();
                    var result = context.Translate<TEntity>(reader).ToList();
                    //foreach (var entity in result)
                    //    Set<TEntity>().Attach(entity);
                    //close up the reader, we're done saving results
                    reader.Close();
                    return result;
                }

            }
        }
12 years ago
I see. Download version 2.30 and replace your NopObjectContext implementation (2.20) with the new one (2.30). Does it help?
12 years ago
a.m. wrote:
I see. Download version 2.30 and replace your NopObjectContext implementation (2.20) with the new one (2.30). Does it help?



P.S. Why did you comment "Set<TEntity>().Attach(entity);"?
12 years ago
a.m. wrote:
I see. Download version 2.30 and replace your NopObjectContext implementation (2.20) with the new one (2.30). Does it help?


P.S. Why did you comment "Set<TEntity>().Attach(entity);"?


I couldn't find any difference between version 2.3 /2.2 (NopObjectContext), they both are same and the Set... line is commented in 2.3 too , It was commented there from the scratch , I havnt commented anything.
Regards
12 years ago
a.m. wrote:
I see. Download version 2.30 and replace your NopObjectContext implementation (2.20) with the new one (2.30). Does it help?


P.S. Why did you comment "Set<TEntity>().Attach(entity);"?


Well replaced the NopObjectContext with the new one , still no difference.
12 years ago
creative3k wrote:
I couldn't find any difference between version 2.3 /2.2 (NopObjectContext), they both are same and the Set... line is commented in 2.3 too , It was commented there from the scratch , I havnt commented anything.

These files are different. 2.30 invokes AttachEntityToContext method instead of the Set... line. And 2.20 doesn't have the Set... line commented (your version has it commented).

It should work fine once replaced by 2.30 NopObjectContext version and recompiled
12 years ago
a.m. wrote:
I couldn't find any difference between version 2.3 /2.2 (NopObjectContext), they both are same and the Set... line is commented in 2.3 too , It was commented there from the scratch , I havnt commented anything.
These files are different. 2.30 invokes AttachEntityToContext method instead of the Set... line. And 2.20 doesn't have the Set... line commented (your version has it commented).

It should work fine once replaced by 2.30 NopObjectContext version and recompiled


Yeh I had replaced it with the new one but still i find productvariants empty


[NonAction]
        private ProductModel PrepareProductOverviewModel(Product product, bool preparePriceModel = true, bool preparePictureModel = true)
        {
            if (product == null)
                throw new ArgumentNullException("product");

            var model = product.ToModel();
            //call the productmisc model
            model.ProductMisc = PrepareProductMiscModel(product);
            //price
            if (preparePriceModel)
            {
                model.ProductPrice = PrepareProductPriceModel(product);
            }
return model;
        }
private ProductModel.ProductMiscModel PrepareProductMiscModel(Product product)
        {
            if (product == null)
                throw new ArgumentNullException("product");

            var model = new ProductModel.ProductMiscModel();
if (product.ProductVariants.Count == 1)
              {
            model.Sku = product.ProductVariants.First().Sku; }
model.productSpecification = product.ProductSpecificationAttributes;
    return model;
        }

product.ProductVariants.Count is always 0
12 years ago
It works fine without any customization. Perhaps, you changed something important that broke the loading of the navigation properties. I don't know what else has been changed. It needs some further investigation.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.