0

I am currently working on building a scanning portion of an app, and noticed that when my eventlistener hits and executes a function, I eventually get the following error:

A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext.

After doing some research here, I learned that I should probably be awaiting those processes, but what I've added doesn't seem to be doing the trick. My threads are still getting tangled. My console shows the issue occuring specifically at:

GetByBarcode(String barcode)

But I'm not sure where else I would need to account for an await operator in that task. Am I missing an await call somewhere, or could the issue actually big bigger than that? Perhaps I've misunderstood await. I've added the relevant portions of code below.

<div @onkeydown="@KeyDown"
 tabindex="0"
 @ref="scanner">
public async Task KeyDown(KeyboardEventArgs e)
{
    barcode += e.Key;
    await CheckBarcode();
}

private async Task CheckBarcode()
{
    testStuff = await stuffRepository.GetByBarcode(barcode);
    if(testStuff is null)
    {
    //
    }
    else
    {
        await stuffRepository.Update(testStuff, loginID);
        await LoadStuff();
        barcode = "";
    }
}

public async Task<StuffDTO> GetByBarcode(string barcode)
    {
        var obj = await _db.StuffTable.FirstOrDefaultAsync(u => u.Barcode == barcode);
        if (obj != null)
        {
            return _mapper.Map<Stuff, StuffDTO>(obj);
        }
        return new StuffDTO();
    }
8
  • Are you awaiting the KeyDown call? I assume youre doing something like @onkeydown="OnKeyDown". If so, then it wants to be @onkeydown="async (e) => await OnKeyDown(e)". Commented Feb 9, 2023 at 14:17
  • Hi @KieranDevlin, if I understand the question, the KeyDown call itself is attached to a div. I've added that code to my question. Commented Feb 9, 2023 at 14:20
  • Yes, try awaiting the method call like I mentioned in my initial comment and see if that solves your problem. Commented Feb 9, 2023 at 14:22
  • Unfortunately, that didn't seem to change anything. Commented Feb 9, 2023 at 14:24
  • Hmm ok, what platform are you running Blazor under? WASM, MAUI Hybrid or Server? Commented Feb 9, 2023 at 14:32

1 Answer 1

2

In the async world you can be awaiting one DbContext operation and trying to start another at the same time. That's what the error is telling you.

You should switch to using the DbContextFactory and separate contexts per operation. The DbContextFactory manages a pool of connections and deals with create and disposing of individual contexts. See this MS article that explains how to configure it - https://learn.microsoft.com/en-us/ef/core/dbcontext-configuration/#using-a-dbcontext-factory-eg-for-blazor.

You should also be building some form of data pipeline to interact with your database, not using Db contexts directly in your components.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the comment. I've begun researching DBContextFactory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.