An entity object cannot be referenced by multiple instances of IEntityChangeTracker

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 лет назад
When creating a thread to run a script I get:

"An entity object cannot be referenced by multiple instances of IEntityChangeTracker”?"

More info on fix:

http://stackoverflow.com/questions/620084/how-to-get-rid-off-an-entity-object-cannot-be-referenced-by-multiple-instances-o

(store data context in HttpContext
13 лет назад
What thread are you creating? What should I do to reproduce the exception?
13 лет назад
We have a script which accepts a CSV and updates all product variant stock levels. Trying to reproduce this for you.

The method which is complaining is:

/// <summary>
        /// Updates the product variant
        /// </summary>
        /// <param name="productVariantId">The product variant identifier</param>
        /// <param name="cycleLength">The cycle length</param>
        /// <param name="stockQuantity">The stock quantity</param>
        /// <param name="productCost">The product cost</param>
        /// <returns>Product variant</returns>
        public static ProductVariant UpdateProductVariant(int productVariantId, int stockQuantity, int cycleLength, decimal productCost)
        {

            var productVariant = GetProductVariantById(productVariantId);
            if (productVariant == null)
                return null;

            var context = ObjectContextHelper.CurrentObjectContext;
            if (!context.IsAttached(productVariant))
                context.ProductVariants.Attach(productVariant);


            productVariant.CycleLength = cycleLength;
            productVariant.StockQuantity = stockQuantity;
            productVariant.ProductCost = productCost;

            context.SaveChanges();

            if (ProductManager.CacheEnabled)
            {
                
            }

            return productVariant;
        }

Iits the attached method which throws an error.

I am going some further tests and will report back.
13 лет назад
Changing the above method to:

public static ProductVariant UpdateProductVariant(int productVariantId, int stockQuantity, int cycleLength, decimal productCost)
        {
            var context = ObjectContextHelper.CurrentObjectContext;
            var productVariant = context.ProductVariants.FirstOrDefault(p => p.ProductVariantId == productVariantId);

            if (productVariant == null)
                return null;

            productVariant.CycleLength = cycleLength;
            productVariant.StockQuantity = stockQuantity;
            productVariant.ProductCost = productCost;

            context.SaveChanges();

            if (ProductManager.CacheEnabled)
            {
                
            }

            return productVariant;
        }

Worked.

I.E it was the attach method which through the error.
13 лет назад
Could you please post source code of your ProductManager.GetProductVariantById(int productVariantId) method here?
13 лет назад
/// <summary>
        /// Gets a product variant
        /// </summary>
        /// <param name="productVariantId">Product variant identifier</param>
        /// <returns>Product variant</returns>
        public static ProductVariant GetProductVariantById(int productVariantId)
        {
            if (productVariantId == 0)
                return null;

            string key = string.Format(PRODUCTVARIANTS_BY_ID_KEY, productVariantId);
            object obj2 = NopRequestCache.Get(key);
            if (ProductManager.CacheEnabled && (obj2 != null))
            {
                return (ProductVariant)obj2;
            }

            var context = ObjectContextHelper.CurrentObjectContext;
            var query = from pv in context.ProductVariants
                        where pv.ProductVariantId == productVariantId
                        select pv;
            var productVariant = query.SingleOrDefault();

            if (ProductManager.CacheEnabled)
            {
                NopRequestCache.Add(key, productVariant);
            }
            return productVariant;
        }
13 лет назад
I've the same problem.
The problem is verified only when running non-webApplication that uses NopCommerce Libraries. (The ASPNetObjectContextManager cannot be used!!!)

Sorry for my bad english!

Best regards
13 лет назад
http://dotnetslackers.com/articles/ado_net/Managing-Entity-Framework-ObjectContext-lifespan-and-scope-in-n-layered-ASP-NET-applications.aspx

this is a great example!
Hope it helps
13 лет назад
I have the exact same issue and I am also trying to update stock levels
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.