Having issues while using IDbContextFactory on grid

Hello,

I was using DbContext in my repositories but after reading this IDbContextfactory I decided to use factory.

Issues:

  1. When updating the vendor name in the grid, the updated vendor does not reflect on the grid. After the page refreshes make it display on the grid. (database is correct)

  2. Lets say when I am updating a row and made some changes, but decided not to update. When I click the cancel, updated data stays on the row, the old original value is not visible. After the page refreshes the original not updated data displays on the grid. (database is correct)

Actually, when I check data from the database, the data is correct, it just looks wrong on the data grid. It fixes itself after refreshing the page. What could be causing this and how can I fix it?

Issues I came across (simplified):

<RadzenDataGrid @ref="_grid" AllowFiltering="true" AllowPaging="true" PageSize="20" AllowSorting="true" RowClick="RowClick" ExpandMode="DataGridExpandMode.Single"
			Data="@_orders" TItem="Order" EditMode="DataGridEditMode.Single" RowUpdate="@OnUpdateRow" RowCreate="@OnCreateRow" @bind-Value="@SelectedOrders"
			ShowExpandColumn="false" ShowPagingSummary="true" AllowColumnResize="true">
<Template Context="order">	
<RadzenDataGrid @ref="_gridDetail" AllowFiltering="@(_detailToInsert == null)" AllowPaging="true" PageSize="15" AllowSorting="@(_detailToInsert == null)" Data="@order.OrderDetails" TItem="OrderDetail" EditMode="DataGridEditMode.Multiple" RowUpdate="@OnUpdateRowDetail" RowCreate="@OnCreateRowDetail" AllowColumnResize="true" AllowColumnPicking="true" ShowPagingSummary="true" ColumnWidth="150px" Density="Density.Compact">
	<Columns>
<RadzenDataGridColumn TItem="OrderDetail" Property="Vendor.Name" Title="Vendor" Width="200px" OrderIndex="4">
	<EditTemplate Context="orderDetail">
		<RadzenDropDownDataGrid TValue="int" AllowFiltering="true" AllowClear="true" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" FilterOperator="StringFilterOperator.Contains" Data=@_vendors Count="5" TextProperty="Name" ValueProperty="Id" Class="w-100" @bind-Value="orderDetail.VendorId" Name="vendor" id="vendor" />
		<RadzenNumericRangeValidator Component="vendor" Min="1" Text="Vendor is Required!" Popup=true Style="position: absolute; z-index: 9999;" />
	</EditTemplate>
</RadzenDataGridColumn>
    <RadzenDataGridColumn TItem="OrderDetail" Property="CustomerOrderNumber" Title="Customer Order" OrderIndex="20">
		<EditTemplate Context="orderDetail">
			<RadzenTextBox @bind-Value="orderDetail.CustomerOrderNumber" Style="width: 100%; display: block" Name="CustomerOrderNumber" />
		</EditTemplate>
	</RadzenDataGridColumn>
	<AuthorizeView Roles="Administrators">
		<RadzenDataGridColumn TItem="OrderDetail" Property="PaymentStatus" Title="Payment Status" OrderIndex="21">
			<EditTemplate Context="orderDetail">
				<RadzenDropDown AllowClear="true" TValue="string" @bind-Value="orderDetail.PaymentStatus" Class="w-100" Data=@paymentStatus Name="PaymentStatus" />
			</EditTemplate>
		</RadzenDataGridColumn>
	</AuthorizeView>
	<RadzenDataGridColumn TItem="OrderDetail" Property="OrderId" Title="Order Id" OrderIndex="22" />
	<RadzenDataGridColumn TItem="OrderDetail" Context="orderDetail" Filterable="false" Sortable="false" TextAlign="TextAlign.Center" Width="200px" OrderIndex="22">
		<Template Context="detail">
			<RadzenButton Icon="edit" ButtonStyle="ButtonStyle.Primary" Class="m-1" Click="@(args => EditRowDetail(detail))" @onclick:stopPropagation="true" Size="ButtonSize.Small">
			</RadzenButton>
		</Template>
		<EditTemplate Context="detail">
			<RadzenButton Icon="check" ButtonStyle="ButtonStyle.Primary" Class="m-1" Click="@(args => SaveRowDetail(detail))" Size="ButtonSize.Small">
			</RadzenButton>
			<RadzenButton Icon="close" ButtonStyle="ButtonStyle.Light" Class="m-1" Click="@(args => CancelEditDetail(detail))" Size="ButtonSize.Small">
			</RadzenButton>
			<RadzenButton Icon="delete" ButtonStyle="ButtonStyle.Danger" Class="m-1" Click="@(args => ShowInlineDialog(detail))" Size="ButtonSize.Small">
			</RadzenButton>
		</EditTemplate>
	</RadzenDataGridColumn>
</Columns>
</RadzenDataGrid>

Some variables:

Enumerable<Order?> _orders = new List<Order?>();
IEnumerable<Vendor?> _vendors;
IEnumerable<Customer?> _customers;
RadzenDataGrid<Order?> _grid;
RadzenDataGrid<OrderDetail> _gridDetail;

Methods:

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

	//userName = user.Identity.Name;
	if (!user.Identity.IsAuthenticated)
	{
		NavigationManager.NavigateTo("/Identity/Account/Login", false);
	}
	if (DataLoading)
	{
		return;
	}

	try
	{
		DataLoading = true;
		await UpdateOrdersIsCompletedUseCase.ExecuteAsync();
		await UpdateOrdersIsContinuesUseCase.ExecuteAsync();
		_vendors = await ViewAllVendorsUseCase.ExecuteAsync();
		_customers = await ViewAllCustomersUseCase.ExecuteAsync();
		_orders = await ViewAllOrdersUseCase.ExecuteAsync(user);
		
	}
	catch (Exception e)
	{
		throw;
	}

	finally
	{
		DataLoading = false;
	}
	SelectedOrders = new List<Order?> { _orders.FirstOrDefault() };
	SelectedStatus = "";

}
async Task EditRowDetail(OrderDetail orderDetail)
{
	await _gridDetail.EditRow(orderDetail);
}

private void CancelEditDetail(OrderDetail orderDetail)
{
	if (orderDetail == _detailToInsert)
	{
		_detailToInsert = null;
	}

	_gridDetail.CancelEditRow(orderDetail);
	CancelOrderDetailUseCase.Execute(orderDetail);
}

public class CancelOrderDetailUseCase : ICancelOrderDetailUseCase
{
	private readonly IOrderDetailRepository _orderDetailRepository;

	public CancelOrderDetailUseCase(IOrderDetailRepository orderDetailRepository)
	{
		this._orderDetailRepository = orderDetailRepository;
	}

	public void Execute(OrderDetail orderDetail)
	{
		_orderDetailRepository.EditCancel(orderDetail);
	}
}

async Task SaveRowDetail(OrderDetail orderDetail)
{

        await _gridDetail.UpdateRow(orderDetail);

}

public async Task UpdateOrderDetailAsync(OrderDetail orderDetail)
	{
	await using var ctx = await _db.CreateDbContextAsync();
	var detail = await ctx.OrdersDetail.FindAsync(orderDetail.Id);
	if (orderDetail != null)
	{
		detail.Quantity = orderDetail.Quantity;
		detail.CostRatio = orderDetail.CostRatio;
		detail.Description = orderDetail.Description;
		detail.ProductCode = orderDetail.ProductCode;
		detail.ProductName = orderDetail.ProductName;
		detail.BuyUnitPrice = orderDetail.BuyUnitPrice;
		detail.ShippingNumber = orderDetail.ShippingNumber;
		detail.Status = orderDetail.Status;
		detail.TotalBuyPrice = orderDetail.BuyUnitPrice * orderDetail.Quantity;
		detail.TotalSellPrice = orderDetail.Quantity * orderDetail.SellUnitPrice;
		detail.SellUnitPrice = orderDetail.SellUnitPrice;
		detail.UnitCost = (orderDetail.BuyUnitPrice * (orderDetail.CostRatio / 100)) + orderDetail.BuyUnitPrice;
		detail.TotalUnitCost = orderDetail.UnitCost * orderDetail.Quantity;
		detail.Currency = orderDetail.Currency;
		detail.CustomerOrderNumber = orderDetail.CustomerOrderNumber;
		detail.CustomerStockCode = orderDetail.CustomerStockCode;
		detail.OrderId = orderDetail.OrderId;
		detail.VendorId = orderDetail.VendorId;
		detail.TrackingNumber = orderDetail.TrackingNumber;
		detail.Warehouse = orderDetail.Warehouse;
		detail.PaymentStatus = orderDetail.PaymentStatus;
		if (detail.Status == "Completed")
		{
			detail.CompletionDateTime = DateTime.Now.Date;
		}
		else
		{
			detail.CompletionDateTime = null;
		}

		ctx.OrdersDetail.Update(detail);
		await ctx.SaveChangesAsync();
	}
}
public void Cancel(OrderDetail orderDetail)
{
    using var ctx = _db.CreateDbContext();

    var orderDetailEntry = ctx.Entry(orderDetail);
    if (orderDetailEntry.State == EntityState.Modified)
    {
        orderDetailEntry.CurrentValues.SetValues(orderDetailEntry.OriginalValues);
        orderDetailEntry.State = EntityState.Unchanged;
    }
}

Try to execute Reload() for the DataGrid.

Didn't help, I think this is related to EF Core. I think I had to bind a DTO to the grid data rather than the entity itself at the beginning. On the repositories, I should convert those DTO to the entity and I will take the relevant action.
What do you think?

I'm having the same issue. When switching to DbContextFactory the data changes are not updated in the Ui until a page refresh occurs. Does anyone know how to resolve this?