实体对象不能被 IEntityChangeTracker 的多个实例引用

发布于 2024-12-04 17:37:05 字数 2440 浏览 0 评论 0原文

我在使用 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)没有太多经验,因此如果可以提供完整的解决方案,那将非常感激。我在互联网上看到了很多此错误的示例,但还没有找到对我有效/有意义的解决方案。根据我的阅读,显然发生的情况是该产品被附加到两个不同的 ObjectContext。我认为这就是这里发生的事情,但我对这项技术了解不够,无法找出发生在哪里/为什么。我尝试在 SaveChanges 后分离,以便下次运行时不会重新附加同一对象,但这并不是我预期的解决方案......

非常感谢,Adrian。

I'm having a problem with nopcommerce 1.9, and really need some assistance please.

The work I'm doing is to add a product importer into the existing functionality.

On first run after an iisreset, the importer runs fine. The second time after however and any further instance produces the above error. This seems to constantly happen when running the following code IoC.Resolve().UpdateProduct(product). The code that this calls is below:

    /// <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 });
    }

I don't have very much experience with this type of technology (ObjectContexts), so if a complete solution could be provided, that would be very much appreciated. I've seen lots of examples of this error across the internet, but haven't found a solution that works/makes sense to me. From what I've read, what is apparently happening is that the product is being attached to two different ObjectContexts. I assume this is what's happening here, but I don't know enough about the technology to find out where/why. I tried detaching after SaveChanges, so that the next time it ran it wouldn't reattach the same object but that hasn't been the solution as I expected it to be...

Many thanks, Adrian.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰葑 2024-12-11 17:37:05

产品是否在回发期间被缓存?通常,当对象是静态的,但最初在初始页面加载时查询它,然后在回发中更新时,就会发生这种情况,类似的情况。发生这种情况的原因是因为它知道从中查询的对象上下文是与您此处的实例不同的实例。

如果您首先使用 Detach 方法分离实体,那么您将能够执行此操作。否则,请提供有关以下内容的更多信息:

  • 对象上下文是否静态存储?
  • 实体是否被缓存?
  • _context 在哪里定义以及如何实例化上下文?

HTH。

Is product being cached across postbacks? Typically that happens when the object is static, but it was originally queried on initial page load and then updated in a postback, something like that. The reason it happens is because the object context it knows that it was queried from was a different INSTANCE than the one you have here.

If you first Detach the entity by using the Detach method, then you'll be able to do this. Otherwise, please provide more information about:

  • Is the object context stored statically?
  • Is the entity cached?
  • Where is _context defined and how are you instantiating the context?

HTH.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文