Total Posts:
187
Karma:
2299
Joined:
11/25/2009
Location:
United Kingdom
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
Total Posts:
16630
Karma:
156065
Joined:
10/22/2008
Location:
Armenia
What thread are you creating? What should I do to reproduce the exception?
Total Posts:
187
Karma:
2299
Joined:
11/25/2009
Location:
United Kingdom
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.
Total Posts:
187
Karma:
2299
Joined:
11/25/2009
Location:
United Kingdom
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.
Total Posts:
16630
Karma:
156065
Joined:
10/22/2008
Location:
Armenia
Could you please post source code of your ProductManager.GetProductVariantById(int productVariantId) method here?
Total Posts:
187
Karma:
2299
Joined:
11/25/2009
Location:
United Kingdom
/// <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; }
Total Posts:
2
Karma:
10
Joined:
9/8/2010
Location:
Italy
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
Total Posts:
2
Karma:
10
Joined:
9/8/2010
Location:
Italy
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
Total Posts:
71
Karma:
675
Joined:
10/6/2010
Location:
Sweden
I have the exact same issue and I am also trying to update stock levels