NopCommerce 4.2 - Tier Price gets deletec after Editing

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

I just upgraded a site from 4.1 to 4.2, and I found the Tier Price gets deleted after editing it. Creation and deletion of the Tier Price work fine. Changing 'Customer Role' along with 'Price' prevents the issue from time to time but not guaranteed.

The IIS version is 10.0.17763.1 and the development environment was Visual Studio 2017 with .NET Core 2.2.1. Then I swapped to Visual Studio 2019 with .NET Core 2.2.5, but it DID NOT solve the issue.

I compared the code with 4.1 and they are identical, then I debugged and worked through the code:

=> \Nop.Web\Areas\Admin\Controllers\ProductController.cs
     method: [Http Post]TierPriceEditPopup(TierPriceModel model)
     Line: 2424 - _productService.UpdateTierPrice(tierPrice);

=> \Nop.Services\Catalog\ProductService.csProductService.cs
     method: UpdateTierPrice(TierPrice tierPrice)
     Line: 1944 - _tierPriceRepository.Update(tierPrice);

=> \nop.data\efrepository.cs
     method: Update(TEntity entity)
     Line: 141 - Entities.Update(entity); --> Works Fine
     Line: 142 - _context.SaveChanges(); --> Where 'Tier Price' gets deleted, deletion query was shown on IIS Express console
4 years ago
I stumbled upon this bug a few weeks ago as well.  I can confirm this is a bug in 4.2 and the delete command is executed as stated above on the save command after the update.
4 years ago
The root cause of this bug is in the TierPriceMap

If you have a tierprice that has "All" customer roles, the update works. If you create a tierprice for a specific role, and then attempt to update, the ondelete cascade behavior runs the delete.

builder.HasOne(price => price.CustomerRole)
                .WithMany()
                .HasForeignKey(price => price.CustomerRoleId)
                .OnDelete(DeleteBehavior.Cascade);
4 years ago
ChuckR wrote:
The root cause of this bug is in the TierPriceMap

If you have a tierprice that has "All" customer roles, the update works. If you create a tierprice for a specific role, and then attempt to update, the ondelete cascade behavior runs the delete.

builder.HasOne(price => price.CustomerRole)
                .WithMany()
                .HasForeignKey(price => price.CustomerRoleId)
                .OnDelete(DeleteBehavior.Cascade);


=========================================================================
Hi ChuckR,

Thanks for sharing the root cause, you are right, the update works when setting the curtomer role to "All". I am not sure if I want to change the OnDelete behavior for TierPriceMapping table though, it supposed to work even with current setting, right? Have you found a way to solve this issue?
4 years ago
Thanks. Here is a work item for this issue.
4 years ago
We could not reproduce the bug for the 4.30 version. it's related to the abandonment of the EF Core.
4 years ago
Hi nopCommerce Team,

I found an interesting fact about this issue.

Let's say:
- I assigned role A (A can be a user-created role or system role) to an administrator, so the admin will be able to check the price of role A on the public store and alter the tier price at the back-end.

When:
- The admin holds the role A and changes the tier price of A, the tier price gets deleted.
- The admin doesn't hold the role A and changes the tier price of A, the tier price gets updated successfully.

I haven't got time to debug this, but hopefully, this will help you guys on fixing the issue in version 4.2 or the coming version.

Cheers,
Ziqi
3 years ago
Can this be looked at for 4.2?  I see the bug request was closed on account that it was "fixed" for 4.3.  But what about any one running 4.2 that cannot upgrade yet?

Has anyone done any more research on this?

Can this issue be related to a problem with EFCore, see https://stackoverflow.com/questions/47334441/entity-framework-core-2-0-deleting-record-instead-of-updating-it
3 years ago
I think it's related to the entity.CustomerRole getting nulled when the Repository Update method is called and the subsequent SaveChanges().

I've been tracking it and when the TierPrice is changed in the controller and the call to save the change is made, the TierPrice contains the customer Role, then it is sent to the Repository and the customer role disappears from the TierPrice, however the rollId remains.

If I comment out the DeleteBehavior.Cascade property in the TierPriceMap, then the entity is updated but the rollId is deleted from the record in the database.  This confirms the Customer Role disappearance is causing the deletion of the TierPrice, but why is the Role being deleted when running the Update Method of the Repository?
3 years ago
if you have access to the db, check the Foreign Key properties and set the Delete Rule to "No Action" if it's set to Cascade

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.