我遇到了nopcommerce 1.9的问题,请真的需要一些帮助。
我正在做的工作是将产品导入器添加到现有功能中。
在iisreset之后首次运行时,导入程序运行正常。然而,第二次和任何进一步的实例产生上述错误。运行以下代码IoC.Resolve()。UpdateProduct(product)时,这似乎经常发生。这个调用的代码如下:
/// <summary>
/// Updates the product
/// </summary>
/// <param name="product">Product</param>
public void UpdateProduct(Product product)
{
if (product == null)
throw new ArgumentNullException("product");
product.Name = CommonHelper.EnsureNotNull(product.Name);
product.Name = CommonHelper.EnsureMaximumLength(product.Name, 400);
product.ShortDescription = CommonHelper.EnsureNotNull(product.ShortDescription);
product.FullDescription = CommonHelper.EnsureNotNull(product.FullDescription);
product.AdminComment = CommonHelper.EnsureNotNull(product.AdminComment);
product.MetaKeywords = CommonHelper.EnsureNotNull(product.MetaKeywords);
product.MetaKeywords = CommonHelper.EnsureMaximumLength(product.MetaKeywords, 400);
product.MetaDescription = CommonHelper.EnsureNotNull(product.MetaDescription);
product.MetaDescription = CommonHelper.EnsureMaximumLength(product.MetaDescription, 4000);
product.MetaTitle = CommonHelper.EnsureNotNull(product.MetaTitle);
product.MetaTitle = CommonHelper.EnsureMaximumLength(product.MetaTitle, 400);
product.SEName = CommonHelper.EnsureNotNull(product.SEName);
product.SEName = CommonHelper.EnsureMaximumLength(product.SEName, 100);
if (!_context.IsAttached(product))
_context.Products.Attach(product);
_context.SaveChanges();
if (this.CacheEnabled)
{
_cacheManager.RemoveByPattern(PRODUCTS_PATTERN_KEY);
_cacheManager.RemoveByPattern(PRODUCTVARIANTS_PATTERN_KEY);
_cacheManager.RemoveByPattern(TIERPRICES_PATTERN_KEY);
_cacheManager.RemoveByPattern(CUSTOMERROLEPRICES_PATTERN_KEY);
}
//raise event
EventContext.Current.OnProductUpdated(null,
new ProductEventArgs() { Product = product });
}
我对这种技术(ObjectContexts)没有太多经验,所以如果能够提供完整的解决方案,那将非常感激。我已经在互联网上看到了很多关于这个错误的例子,但是没有找到一个对我有用/有意义的解决方案。从我所读到的,显然正在发生的是该产品被附加到两个不同的ObjectContexts。我认为这就是这里发生的事情,但我对该技术的了解不足以找出原因/原因。我尝试在SaveChanges之后进行分离,以便下次运行它时不会重新连接相同的对象,但这不是我预期的解决方案......
非常感谢,阿德里安。
答案 0 :(得分:1)
产品是否在回发中缓存?通常情况下,当对象是静态时会发生这种情况,但最初在初始页面加载时查询它,然后在回发中更新,就像这样。它发生的原因是它知道它所查询的对象上下文是一个不同于你在这里的INSTANCE。
如果您首先使用Detach
方法分离实体,那么您将能够执行此操作。否则,请提供有关以下内容的更多信息:
HTH。