Both parent and child have to access db context in order to get their specific data, bellow is their code.
Parent:
[Inject]
private IProductsService ProductService { get; set; }
private IEnumerable<ProductModel> ProdList;
private bool FiltersAreVisible = false;
protected override async Task OnInitializedAsync()
{
ProdList = await ProductService.GetObjects(null);
}
Child:
[Parameter]
public IEnumerable<ProductModel> ProdList { get; set; }
[Parameter]
public EventCallback<IEnumerable<ProductModel>> ProdListChanged { get; set; }
[Inject]
private IRepositoryService<ProdBusinessAreaModel> ProdBAreasService { get; set; }
[Inject]
private IRepositoryService<ProdRangeModel> ProdRangesService { get; set; }
[Inject]
private IRepositoryService<ProdTypeModel> ProdTypesService { get; set; }
[Inject]
private IProductsService ProductService { get; set; }
private ProductFilterModel Filter { get; set; } = new ProductFilterModel();
private EditContext EditContext;
private IEnumerable<ProdBusinessAreaModel> ProdBAreas;
private IEnumerable<ProdRangeModel> ProdRanges;
private IEnumerable<ProdTypeModel> ProdTypes;
protected override async Task OnInitializedAsync()
{
EditContext = new EditContext(Filter);
EditContext.OnFieldChanged += OnFieldChanged;
ProdBAreas = await ProdBAreasService.GetObjects();
ProdRanges = await ProdRangesService.GetObjects();
ProdTypes = await ProdTypesService.GetObjects();
}
This is throwing the following exception: InvalidOperationException: A second operation was started on this context before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext.
Using break points I see that parent runs OnInitializedAsync
and when reaches ProdList = await ProductService.GetObjects(null);
jumps right away to child OnInitializedAsync
.
I solved it by making all the requests from parent and then passing to child but I wonder if there is a better way to do this, leaving child with the ability to get its own data and of course without making DB context Transient..
Regards