'Entity Framework get real-time value from database

I have a nopcommerce website. I find a problem. Please see below code

// My account / Order details page
[HttpsRequirement(SslRequirement.Yes)]
public virtual IActionResult Details(int orderId)
{
    var order = _orderService.GetOrderById(orderId);

    if (order == null || order.Deleted || _workContext.CurrentCustomer.Id != order.CustomerId)
        return Challenge();

    var orderTotal = order.OrderTotal;

    order = _orderService.GetOrderById(orderId);
    var orderTotal2 = order.OrderTotal;
    var model = _orderModelFactory.PrepareOrderDetailsModel(order);

    return View(model);
}

When I put a breakpoint on

var orderTotal = order.OrderTotal; 

I get the value (100) of orderTotal from table Order. Then I changed the value of orderTotal from 100 to 200 in the database, and continue to execute the code.

order = _orderService.GetOrderById(orderId);
var orderTotal2 = order.OrderTotal;

This code should get the new value (200) of orderTotal from the table Order, however, it still returns a value of 100 for orderTotal2.

I want to get the refreshed data in function in the controller. Could you help me solve this problem?



Solution 1:[1]

Because the GetOrderById Method retrieves the cached order in the memory using 'ToCachedGetById', you can use GetOrderByGuid method instead to get the order directly from DB and skip the caching order

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Omar