0

I have a Blazor page where I could edit a child record in a structure like

Mastertable (one -> many) child table (one -> many) childchild table.

Every table are connected to some lookuptables/data.

My editpage for Mastertable work fine and also my editpage for childchild table.

On my headpage I could choose to list all Masterrecords, I have a list where I could list all records in child table and so on. Here I could without any error click on my link that take me to /editchild/{id}

But if I'm at mastertable edit page and list childrecords, i could click on my link that take me to /editchild/{id}. But than it crash on my rows where I load my lookuptabledata in async Task OnInitializedAsync().

lookupTableAList = await ARepository.GetAllAsync();
lookupTableBList = await BRepository.GetAllAsync();
lookupTableCList = await CRepository.GetAllAsync();
lookupTableDList = await DRepository.GetAllAsync();
masterTableList = await MasterRepository.GetAllAsync();  // Get all records in mastertable so I can link to another masterRecord with a dropdown

Anyone who has a clue why it works if I came from my headpage without any records loaded but if I call my childedit page from a page where my masterdata is loaded, it crash?

Repository for lookuptables like (get from a generic repository):

public virtual async Task<List<T>> GetAllAsync()
{
    return await _db.Set<T>().ToListAsync();
}

MasterRepository like :

public async Task<IEnumerable<MasterEntity>> GetAllAsync()
{
    IEnumerable<MasterEntity> masterEntity = await _db.MasterEntity
        .Where(u => !u.IsDeleted)
        .OrderBy(o => o.Name)
        .ToListAsync();

    return masterEntity;
}

Latencyproblems, conflict from one page to another in Blazor, generic repository problems or something else?

EDIT : If I place await Task.Delay(1000); between every Repository.GetAllAsync(); it won't crash when I call this editpage from edit masterrecord page. But that's not a nice solution.

I think I'm found my problems but not a solution. In my Masteredit page the childtable using Radzen Grid as a master-child connection with selection turned on. So when I click on my link, at the same time it activate a selectrow call that make a call to repository on Masteredit page, so I got an threadconflict to my dbcontext.

Is there a way to get my loaded childpage to wait for thread on masterpage to be finished?

  • This is pretty difficult to understand, but if adding a Task.Delay between the await calls solves the problem then that suggests that `public async Task GetAllAsync()` isn't actually behaving asynchronously, and `ToListAsync()` is a block of synchronous code pretending to be asynchronous. What Database are you using? Does it work with `Task.Delay(1);`? – MrC aka Shaun Curtis Aug 30 '22 at 13:11
  • I'm using MS SQL Server. I tried with Task.Delay(1), but won't work. Maybe set Task.Delay(1) in repository? – Tobias Gustafsson Aug 30 '22 at 18:23
  • OK, so on that basis forget my earlier comment. You probably need to consider using a view service to hold your data and then events to signal that updates have taken place. Take a look at my answer to this post which explains how to implement a view service with notifications through events. https://stackoverflow.com/questions/69558006/how-can-i-trigger-refresh-my-main-razor-page-from-all-of-its-sub-components-wit, It should give you some ideas about how to solve your problem. – MrC aka Shaun Curtis Aug 30 '22 at 22:14
  • Can you post the error trace? – dani herrera Sep 04 '22 at 16:30
  • After some frustration I found that the ground problem were that my db context is Scoped, I tried to make it transient and then it works. So I need to clean up in my code, and maybe look at db factory. – Tobias Gustafsson Sep 07 '22 at 10:44

0 Answers0