I have a Blazor .net 8 Web App where I use Entity Framework Core with SQL Server. I have the following code for my view page:
view.razor.cs:
public partial class View : ComponentBase
{
[Parameter]
public int DebtorId { get; set; }
private Debtor _debtor = new();
private bool DataLoading = false;
private HubConnection _hub;
protected override async Task OnInitializedAsync()
{
if (DataLoading)
{
return;
}
try
{
DataLoading = true;
await RefreshData(true);
_hub = new HubConnectionBuilder()
.WithUrl(NavigationManager.ToAbsoluteUri("/dmhub"))
.Build();
_hub.On<string, int>("CUD", (Model, Id) =>
{
if (Model == "debtor" && Id == DebtorId)
{
InvokeAsync(() => RefreshData());
}
});
await _hub.StartAsync();
}
catch (Exception)
{
throw;
}
finally
{
DataLoading = false;
}
}
private async Task RefreshData(bool? firstrender = false)
{
_debtor = await _debtorRepos.GetSingleOrDefaultWhereIncludeAsync(x => x.Deleted == false && x.Id == DebtorId, "Contacts");
StateHasChanged();
}
Repository.cs:
public class Repository<T> : IRepository<T> where T : class
{
private ApplicationDbContext _db;
public Repository(ApplicationDbContext db)
{
_db = db;
}
public async Task<T> GetSingleOrDefaultWhereIncludeAsync(Expression<Func<T, bool>> where, string include)
{
return await _db.Set<T>().Include(include).SingleOrDefaultAsync(where);
}
}
Whenever I update the name from for example “test” to “test 1 2 3”, the view page receives the message correctly but still shows “test” as name. Only after I hit F5, it shows the new name. I used this same technique in my Blazor Server .net 7 application and it all worked fine there. But now, the new Debtor is only showing after I hit F5.
Anyone who knows what I am doing wrong here?