EF Core 6 updated data retrieve problem with multiple repositories

Viewed 52

I am working on a Blazor Server Application that has a Radzen master-detail data grid. This data grid is populated with IsActive = 1 data OnInitializedAsync method.

Here is the Order repository and related query which retrieves active data:

namespace IMS.Plugins.EFCore
 {
     public class OrderRepository : IOrderRepository
     {
         private readonly IMSContext _db;
            
         public OrderRepository(IMSContext db)
         {
             _db = db;
               
         }
    
         public async Task<IEnumerable<Order?>> GetAllOrders(ClaimsPrincipal user)
         {
             if (user.IsInRole("Administrators"))
             {
                 return await _db.Orders.Include(d => d.OrderDetails.Where(od => od.IsActive == 1)).ThenInclude(v => v.Vendor).ToListAsync();
             }
                
             return await _db.Orders.Include(d => d.OrderDetails.Where(od => od.IsActive == 1)).ThenInclude(v => v.Vendor).ToListAsync();
    
         }         
    
           
     }
 }

Here is the Detail repository which sets the related order detail to IsActive = 0

namespace IMS.Plugins.EFCore
 {
     public class OrderDetailRepository : IOrderDetailRepository
     {
         private readonly IMSContext _db;
    
         public OrderDetailRepository(IMSContext db)
         {
             _db = db;
         }
    
    
         public async Task PassiveOrderDetailAsync(OrderDetail orderDetail)
         {
               
             var detail = await this._db.OrdersDetail.FindAsync(orderDetail.Id);
             if (detail != null)
             {
                    
                 detail.IsActive = 0; // 0-Passive
                    
                 await _db.SaveChangesAsync();
             }
         }
           
     }
 }

Here is the master-detail data grid populated OnInitializedAsync method. By the way, this part is working. (gets IsActive = 1)

protected override async Task OnInitializedAsync()
    {
        user = (await _authenticationStateProvider.GetAuthenticationStateAsync()).User;

        //userName = user.Identity.Name;
        if (!user.Identity.IsAuthenticated)
        {
            NavigationManager.NavigateTo("/Identity/Account/Login", false);
        }
        _orders = await ViewAllOrdersUseCase.ExecuteAsync(user);
        SelectedOrders = new List<Order?> { _orders.FirstOrDefault() };
        _vendors = await ViewAllVendorsUseCase.ExecuteAsync();
        _customers = await ViewAllCustomersUseCase.ExecuteAsync();
    }

The problem starts when I try to update a detail to IsActive = 0 as shown on the screenshot. enter image description here

Related Blazor:

<RadzenButton Icon="delete" ButtonStyle="ButtonStyle.Danger" Class="m-1" Click="@(args => PassiveDetail(detail))">
                                </RadzenButton>

Here is what I do in the related portion:

RadzenDataGrid<OrderDetail> _gridDetail;
    IEnumerable<Order?> _orders = new List<Order?>();
    ...
    async Task PassiveDetail(OrderDetail orderDetail)
        {
            if (orderDetail == _detailToInsert)
            {
                _detailToInsert = null;
            }
    
            await _gridDetail.UpdateRow(orderDetail);
            await PassiveOrderDetailUseCase.ExecuteAsync(orderDetail);
            _orders = await ViewAllOrdersUseCase.ExecuteAsync(user);
            
            StateHasChanged();       
    
        }

Selected row updated successfully but when this line calls _orders = await ViewAllOrdersUseCase.ExecuteAsync(user); it still gets the old data IsActive = 0. I couldn't find out why? However, OnInitializedAsync method works as excepted. Frankly, I couldn't solve.

Edit 1

Is it because there are 2 separate repositories for both order and order details? They are having their own insert and updates?

Edit 2

I should have added earlier Order and OrderDetail entities:

public class Order
{
    public int Id { get; set; }
    
    [Required]
    public DateTime OrderDateTime { get; set; }
    [Required]
    [MaxLength(250)]
    public int CustomerId { get; set; }
    public string Status { get; set; }
    [MaxLength(50)]
    public string DoneBy { get; set; }
    public List<OrderDetail> OrderDetails { get; set; }
    public Customer Customer { get; set; }


}

public class OrderDetail
{
    public int Id { get; set; }
    
    [Required]
    [MaxLength(100)]
    public string ProductCode { get; set; }
    [Required]
    [MaxLength(250)]
    public string ProductName { get; set; }
    [Required]
    public int Quantity { get; set; }
    [Required]
    public double BuyUnitPrice { get; set; }
    public double CostRatio { get; set; }
    public double UnitCost { get; set; }
    public double TotalBuyPrice { get; set; }
    public double? SellUnitPrice { get; set; }
    public double? TotalSellPrice { get; set; }
    [MaxLength(150)]
    public string? ShippingNumber { get; set; }
    public string? Status { get; set; }
    [MaxLength(150)]
    public string? TrackingNumber { get; set; }
    [MaxLength(400)]
    public string? Description { get; set; }
    public string? Currency { get; set; }
    public string? CustomerStockCode { get; set; }
    public string? CustomerOrderNumber { get; set; }
    public int IsActive { get; set; }
    public double? TotalUnitCost { get; set; }
    public int OrderId { get; set; }
    public int VendorId { get; set; }
    public Order Order { get; set; }
    public Vendor Vendor { get; set; }
   
}

Edit 3

I think I found the suspect. The query below shouldn't get the IsActive=0 but it somehow gets! Any ideas for this situation?

Here is the query:

public async Task<IEnumerable<Order?>> GetAllOrders(ClaimsPrincipal user)
    {
        if (user.IsInRole("Administrators"))
        {
            return await _db.Orders.Include(d => d.OrderDetails.Where(od => od.IsActive == 1)).ThenInclude(v => v.Vendor).ToListAsync();
        }
        
        return await _db.Orders.Include(d => d.OrderDetails.Where(od => od.IsActive == 1)).ThenInclude(v => v.Vendor).ToListAsync();

    }

The record is updated, why is the query doesn't work the way it is expected? First I am updating then I am querying IsActive = 1

await PassiveOrderDetailUseCase.ExecuteAsync(orderDetail); //sets to IsActive = 0
_orders = await ViewAllOrdersUseCase.ExecuteAsync(user); // calls GetAllOrders method above,should get only the actives IsActive = 1

But IsActive = 0 also comes inside of _orders as seen in the screenshot below. enter image description here

Edit 4

When I add AsNoTracking() the query in my previous post gets only IsActive = 1 that is what I want but somehow doesn't get the Customer which is why I want to include it. Order have 2 children, Customer and OrderDetail. OrderDetail has one, Vendor. Couldn't manage to include Customer tough to the query.

This works so far.

Orders
                .Include(d => d.OrderDetails.Where(od => od.IsActive == 1))
                .ThenInclude(v => v.Vendor)
                .Include(c => c.Customer)                    
                .AsNoTracking()
                .ToListAsync();
0 Answers
Related