3
votes

I makes the NavMenu dynamically and return menu i the database by users and in the index page already i returned something in the database but when i run the application or reload it show me bellow error

InvalidOperationException: A second operation started on this context before a previous operation completed. This is usually caused by different threads using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913.

NavMenu code,

List<Menu> menus = new List<Menu>();

protected override async Task OnInitializedAsync()
{ 
    menus  = await MenuService.GetMenus();

}

Index code

@if (priorities == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <table class="table">
        <thead>
            <tr>
                <th>Name</th> 
            </tr>
        </thead>
        <tbody>
            @foreach (var priority in priorities)
            {
                <tr>
                    <td>@priority.Name</td>
                </tr>
            }
        </tbody>
    </table>
}

@code { 
    List<Priority> priorities;

    protected override async Task OnInitializedAsync()
    { 
        priorities = await PriorityService.GetPriorities();

    }
}
3
The problem is not in the code you have posted.Henk Holterman
Show GetMenus() and GetPriorities() and how they get and use the dbContext.Henk Holterman
It looks you are sharing same instance of dbcontext for both db requests. If you are using DI, check if for you is possible to work with transient instead scoped: stackoverflow.com/a/41924039/842935dani herrera
Wow. Thank you dani herrera, is working .user3903484
Ok. I post it as solution.dani herrera

3 Answers

6
votes

The solution is to use a `DbContextFactory :

Quoting docs:

Some application types (e.g. ASP.NET Core Blazor) use dependency injection but do not create a service scope that aligns with the desired DbContext lifetime. Even where such an alignment does exist, the application may need to perform multiple units-of-work within this scope. For example, multiple units-of-work within a single HTTP request.

In these cases, AddDbContextFactory can be used to register a factory for creation of DbContext instances. For example:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContextFactory<ApplicationDbContext>(
        options =>
            options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test"));
}
0
votes

I got this error only when I attempted to access the user's information from the same service in multiple controls. Makes sense-- they are both fighting for the same resource on initialization.

For me, the solution wasn't to change the scope-- it was to re-think what I was really doing-- why have 2 separate controls trying to access Membership features for the same user at all?

-2
votes

My solution for a Blazor server app was to make the ApplicatonDbContext and the service both Transient:

services.AddDbContext<ApplicationDbContext>(x => x.UseSqlServer(connectionString), ServiceLifetime.Transient);
services.AddTransient<ICustomerService, CustomerService>();